Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Data to Email loop not working

Status
Not open for further replies.

cbrown131

Programmer
Joined
May 14, 2003
Messages
3
Location
US
I have a number of form pages with about 25 fields each. I am wanting to email the data collected in these forms and have been trying to accomplish this by looping through the form posted variables so that I don't have to manually grab each field for each form. Everything in the script works except the loop. I am fairly new to PHP and any help would be greatly appreciated.

Here is my code:

<?
$to = &quot;jhandy@test.com&quot;;
$subject = &quot;New Form Submission&quot;;
$from = &quot;webmaster@test.com&quot;;
$message = &quot;A new form has been submitted from the website:\n\n&quot;;
do {
$message .= key($_Request) . &quot;: &quot; . $_Request[key($_Request)] . &quot;\n&quot;;
} while(next($_Request));
mail($to, $subject, $message, &quot;FROM: $from&quot;);
?>

Thanks!
 
try this:

foreach($HTTP_POST_VARS as $key=>$value) {
message .= $key.':'.$value.&quot;\n&quot;;
}

andreas owen
aowen@swissonline.ch
 
Andreas,

Thanks for the reply. Unfortuneately this did not work either. It stops processing at this loop for some reason. With your code, it didn't even send the mail with the &quot;a new form has been submitted&quot; like I get with the code I originally posted in this thread. Any other suggestions?
 
You use the $_REQUEST superglobal array. Andreas57 used specifically the POST vars.

It depends on how you send the form data to the script, the method attribute of the <form> tag determines how they appear in PHP (according to the delivery via HTTP).

$_REQUEST is a combination of POST, GET and other variables depending on your version of PHP. I'd go with the $_POST and set the method to POST.

The loop that andreas57 writes is fine. You might just have to change the array that is being iterated:
Code:
foreach($_REQUEST as $key=>$value) {
 message .= $key.':'.$value.&quot;\n&quot;;
}

Again, I'd change the method to POST or with $_REQUEST you'll get $_FILES, $_COOKIE etc. and might not want those.
 
DRJ478,

That do you mean by &quot;change the array that is being iterated?&quot; Forgive my ignorance, but I am just learning this stuff. I don't believe I have an array in my code. What I posted initially is the only PHP code I am using. Am I supposed to have something else there as well? Thanks!
 
$_REQUEST is an array. It is a multidimensional variable that is keyed by the fieldnames in your form.

Let's say you have the following tag in your HTML:
Code:
<input type=&quot;text&quot; name=&quot;mytext&quot;>
<input type=&quot;text&quot; name=&quot;anotherText&quot;>

The user enters Hello World into the textfield called mytext and the numeral 6 into the field named anotherText and submits the page to your PHP script.

In your PHP the $_REQUEST array contains the following information in abstract terms:
fieldname is the array key and value is the value of the array element.
Code:
echo($_REQUEST[mytext]);
prints Hello World.
Code:
echo($_REQUEST[anotherText]);
prints 6.

The foreach loop in the example posted goes element by element through the array and assigns the key (fieldname) to $key and the passed value to $value. The process of looping through the array is called iteration.

Ok?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top