0

I'm trying to setup a simple website with a form that sends to a specific email address. The idea is to have a few different things to be sent in an email, like: first and last name, age, email, city, subject (which is a dropdown menu with a few different options) and then finally a comment box.

I've actually spent the last 14 hours reading and looking up a way of how to get the PHP script to work properly so that the form will be send. But for some reason I just can't figure it out.

I just cant seem to get the form to send with everything included that I require to be included. Could somebody point me out why the below code doesn't work?

<?php
/* EMAIL RECIPIENT */
$myemail = "name@example.com";

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
    show_error($problem);
}
return $data;
}

/* Check all form inputs using check_input function */
$formName = check_input ($_POST['formName'], "Please enter your First Name");
$formSurname = check_input ($_POST['formSurname'], "Please enter your First Name");
$formAge = check_input ($_POST['formAge'],);
$formEmail = check_input ($_POST['formEmail'], "Please enter your email so we can get back to you");
$formIntent = check_input ($_POST['formIntent'], "Pleas choose a reason for why contact us");
$formIntent = " (MYFORM) " . $formIntent;
$formComment = check_input ($_POST['formComment']);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $formEmail))
{
show_error("E-mail address not valid");
}

/* The layout for the email and how it will be set up for reading the email. */

$message = "Hello!

Your contact form has been submitted by:

Name: $formName
Age: $formAge
City: $formCity
Email: $formEmail
Comment: $formName
The reason for contact with Mistral is: $formIntent
Comment: $formComment

End of message.
";

/* Send the message using the mail() function */
mail($myemail, $subject, $message);

/* Redirect visitors to the thank you page */
header('Location: ../thanks.html');


function show_error($myError)
{?>
<html>
<body>

<b>Please correct the following error:</b><br />
<?php echo $myError; ?>

</body>
</html>

<?php
exit();
}
?>
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • 1
    what is "part of a form" and what are you expecting to see. – AD7six Feb 13 '13 at 22:21
  • Can you tell us exactly what part doesn't work? Does the email get send at all or not? Any errors on the page? Etc. Also emailaddress validation: [You're doing it wrong](http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php/12026863#12026863). – PeeHaa Feb 13 '13 at 22:27
  • Filter emails with the following: filter_var($email, FILTER_VALIDATE_EMAIL) As for the problem what is the part that isn't displaying correctly? – Steven10172 Feb 13 '13 at 22:28
  • You're also displaying `Comment: $formName` when it's already displayed as `Name: $formName` – David Feb 13 '13 at 22:29

1 Answers1

0

Well, you validate $_POST['formSurname'] but then you never use it. Similarly, you output $formCity in your $message but you never defined it.

This is most likely the cause of your problems.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592