Page 1 of 1

Forms, dynamic fields

Posted: Sun Dec 10, 2006 7:12 pm
by thiscatis
Hi,

Is there a way to get all the names of a random form field and put them in an array?

e.g.

A user creates a random form

Code: Select all

<form id="form1" name="form1" method="post" action="">
  <input type="text" name="textfield" /><br />
  <textarea name="textarea" cols="10" rows="5"></textarea>
  <input name="checkbox" type="checkbox" value="" />
  <input name="" type="button" />
</form>
And with some php code it'll get the form field names.
So i'll have an array with textfield, textarea,checkbox.

Posted: Sun Dec 10, 2006 11:41 pm
by Sloth

Code: Select all

foreach ($_POST as $key => $randomstuff) {
echo $key . " :: " . $randomstuff // Your random stuff is your 'dynamic' fields, you prolly wanna exlude submit from this
}

Posted: Mon Dec 11, 2006 5:48 am
by aaronhall
All of that information is available in $_POST as form_element_name => form_element_value

Posted: Mon Dec 11, 2006 9:46 am
by thiscatis
I'm currently using this code:

Code: Select all

<?php if($_POST['submit']) {

		foreach ($_POST as $key => $form_content) {

$mailbody = $key . ":" . $form_content . "<br />";  
echo $mailbody; 
$to = "//";
$subject = "Form Submission";
$body = $mailbody;
mail($to, $subject, $body);

 
 }
 	}
	 ?>
But this sends out multiple emails instead of one email with the mailbody.
This is because the "foreach" I think.
If I close the foreach sooner, it just sends one email, but not with the full content.
Anyone knows how to fix that, so there's just one email with the full body.

Posted: Mon Dec 11, 2006 11:11 am
by aaronhall
You obviously can't have mail() inside the loop... all you need inside the loop is

Code: Select all

$mailbody .= $key . ":" . $form_content . "<br />";