Re: Send form data with PHP
Kurt:
The process is fairly simple. When using PHP to process data using the $_GET or $_POST method (POST is generally better when sending lots of data as there are some size limitations using the $_GET method), PHP stores the data in a special array for processing. The number of parameters you are dealing with is usually irrelevant unless the sheer size and number of variables and the time required validating your data causes PHP to exceed the threshold time limit in the php.ini file.
As to the question of sending the data from your form, your question is a little vague. Are you referring to processing the data and then sending it in an email or are you referring to the sequence of sending the data from the form to the script that handles the data?
If you are referring to sending mail with the form then you would create a script to validate and process the form variables. Remember, never trust any user input.
If the validation is acceptable you will come to a place in your processing script where you will format the data and use the PHP mail function to send it out. That portion of the script will look something like this:
$sent_from_email = 'sentfrom@myemailaccount.com';
$toaddress = 'my_email_address@myemailaccount.com';
$subject = 'Question from Contact Us Page ' .$email;
$mailcontent = 'Name: '.$first . $space . $last."\n"
."\n"
.'IP Address of Requestor: '.$ip."\n"
."\n"
.'Email Address: '.$email."\n"
."\n"
.'Comments: '.$comment."\n";
$fromaddress = 'From: .$sent_from_email;
mail($toaddress, $subject, $mailcontent, $fromaddress);
Just modify the above and replace the variables and formatting with the variables from your form and the formatting of your choice. Please make note of a few things. The script above will be sending a text email. The $mailcontent variable is combining the basic formatting and data variables using concatentation. You should also take additional steps prior to sending the email to make sure your mail form has not been hijacked to send spam.
Finally, after dealing with all the security issues, and sending the email, you need to decide what to do. I usually choose to redirect to a new page, though the option to display a thank you message on the same page is of course your decision.
If you decided to do a page redirect you could add some code beneath the mail function as follows:
$URL="thank_you.php";
header ("Location: $URL");
exit;
The above redirect should work perfectly unless you have already sent data to the page (if so, you will get a PHP error telling you headers have already been sent). If the page doesn't redirect, it will likely be the result of outputting headers somewhere earlier in your script. This could be as simple as sending a blank space to the browser before processing the form. There are special functions in PHP that can deal with this but it is just a easy to remove the offending data.
I hope this helps.
__________________
I use Country IP Blocks as added security for my networks and servers.
Last edited by Tech Manager; 01-07-2008 at 11:53 AM.
|