Jump to content

Translation from perl to php


Recommended Posts

Started with an html form and entered a bunch of variables into it. For example _01fname, _02lname, _03address, _03email. With perl we did

use CGI;$query = new CGI;# bundle up form submissions into string variable mail_bodyforeach $field (sort ($query->param)) {	foreach $value ($query->param($field)) {		$mail_body .= "$field: $value\n";	}}

Starting with an html form with variables (names) and values, the code above concatenated all the variable names and their values (_01fname, _02lname, and their values) into a single variable, $mail_body. After that, we opened mail, and printed $mailbody to mail. Example: print MAIL "$mail_body"; This would send me an email with all the variables that were entered into the form, without me having to do print MAIL "first name: $_01fname"; print MAIL "last name: $_02lname"; print MAIL "address: $_03address"; etc, for every single variable name and variable value.So that was how it was done in perl. My question is just how do i bundle up all the variables into one, using php instead of perl. What do i put in the for loop or while loop or whatever kind of loop? I know I can build an array or even use a single long string variable consisting of the input variables concatenated. But how do i get the loop to start at $_01fname, do $_02lname next, and end at $_11anyquestions ??

Link to comment
Share on other sites

OK, I did a lot of googling around and finally found some tutorials that I could understand. I also did some trial and error.What I found that worked is

while (list($key,$value) = each($recip)) { 	$mailbod[$key]="$key: $value"; // bundles up variables into an array variable$thebod.="$key: $value <br>"; //bundles up variables into a single variable}

That bundled up all the elements of the array into a single scalar variable, $thebod. Also, to avoid confusion, I gave each radio button value its own variable name. Then I made $thebod the body of an emal message, by doing

mail($to, $subject, $thebod, $theheaders )

That last part was easier than perl - where I had to open MAIL, then print $thebody to mail, then CLOSE mail.By doing $mailbod[$key] = "$key; $value"; I got an array. I could also have made $thebod outside the while-loop, by simply imploding that array that I made within the while loop.With $mailbod[any element] I would be able to access elements individually, if I wanted to. If I didn't forsee any need for that, I could have simply skipped making the array, and made only the scalar variable. I am going to try adservio's suggestion tho. However I will have to look up $_REQUEST.Here is the resulting form.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...