Mmm, now that is strange…
…the script works on both my test server and my live server…
below is an upgraded version of the script, it checks each value is set and if so it will email you the details (remember to put your email address and website in the script)
if it fails it should tell you exactly what value is missing…
it also performs a little cleanup on each value to help combat naughty people who will try to violate your site with dodgy code…
[php]
Form and Feedback
<?php #script 2.5 handle_form.php #4
//print the submitted information:
if (isset($_POST['submit']))
{
// initialise an array for any errors.
$errors = array();
// do some cleaning.
if(!empty($_POST['name'])) { $name = trim(stripslashes($_POST['name'])); }
else { $name = false; $errors[] = 'Please enter your name'; }
// do some cleaning.
if(!empty($_POST['email'])) { $email = trim(stripslashes($_POST['email'])); }
else { $email = false; $errors[] = 'Please enter your email'; }
// do some cleaning.
if(!empty($_POST['comments'])) { $comments = trim(stripslashes($_POST['comments'])); }
else { $comments = false; $errors[] = 'Please enter a comment'; }
if(isset($_POST['gender'])) { $gender = $_POST['gender']; }
else{ $gender = false; $errors[] = 'Please enter your gender'; }
if(isset($_POST['age'])) { $age = $_POST['age']; }
else { $age = false; $errors[] = 'Please enter your age'; }
// check the errors array is empty (no errors)
if(empty($errors))
{
// create an email to myself. (from my website)
$to = '
[email protected]';
$from = 'From:
[email protected]';
$subject = 'You have been contacted via the form online';
$message = "You have been sent a message from:\n";
$message .= "Name: $name\n";
$message .= "Email: $email\n";
$message .= "Gender: $gender\n";
$message .= "Age: $age\n";
$message .= "Comments: $comments\n";
// send the message.
mail($to, $subject, $message, $from);
echo "
Thank you, $name, for the following comments:
$comments
We will reply to you at $email.
\n";
}
else // there was an error somewhere..
{
echo '
Please go back and correct the following:
';
// loop through the errors array and print them out.
foreach($errors as $error)
{
echo '
' . $error . '
';
}
}
}
else
{
// leave php and display the form using html ?>
Enter your information in the form below
Name:
Email address:
Gender: Male
Female
Age:
Under 30
Between 30 and 60
Over 60
Comments:
<?php
} // back to php to close things off
?>
[/php]
replace the entire contents of your page with this script and let me know how you get on.
Red