Forms, dynamic fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Forms, dynamic fields

Post 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.
Sloth
Forum Newbie
Posts: 18
Joined: Thu Dec 07, 2006 7:29 pm

Post 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
}
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

All of that information is available in $_POST as form_element_name => form_element_value
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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.
Last edited by thiscatis on Fri Dec 29, 2006 7:04 pm, edited 1 time in total.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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 />";
Post Reply