Jump to content

Using a form with PHP to send data to an email


mohorter

Recommended Posts

I am trying to create a form with PHP to send the data to my email, but can't quite get it to work. I have been successful on the php code to send me an email on the data that is supposed to come trough but all it sends is the description of the form fields. Was wondering if anyone out there can help me in where I have messed up, or what I am missing in my code.

 

This is just a shell of what i want to do, but want to try to get it to work before i make it more advanced.

 

HTML Form

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form method="post" name="myemailform" action="email.php">
<p>
<label for="name">Enter Name</label><br>
<input type="text" name="name"><br>
</p>
<br>
<p>
<label for="visitor_email">Enter Email</label><br>
<input type="text" name="visitor_email"><br>
</p>
<label for="times">Enter Times</label><br>
<select name="times">
<option value="3 pm">3 PM</option>
<option value="4 pm">4 PM</option>
</select>
<label for="message">Enter Message</label><br>
<textarea name="message" rows="10" cols="30">Insert comments here</textarea>
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>

 

 

PHP code

<?php
if (!isset($_POST["submit"]))
{
echo "error; you need to submit the form!";
}
$name = $_post["name"];
$visitor_email = $_post["visitor_email"];
$times = $_post["times"];
$message = $_post["message"];
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
$email_from = "adam@email.com";
$email_subject ="New Form Submission";
$email_body ="You have received a new message from $name \n".
"email address: $visitor_email \n".
"Here is the message: $message \n".
"Here is the time: $times \n".
$to = "adam@email.com";
$headers = "from: $email_from \r\n";
mail ($to, $email_subject, $email_body, $headers);
?>

 

 

Thanks in advance

 

Adam

Link to comment
Share on other sites

$_POST has to be written all in capital letters. These variables will not contain anything:

$name = $_post["name"];
$visitor_email = $_post["visitor_email"];
$times = $_post["times"];
$message = $_post["message"];
Link to comment
Share on other sites

Ok so i've updated my page with the form that i want now, but can't seem to get the update to send me the email. Ive updated the capital letters issue, but can't seem to figure out where in the php code where i have screwed up.

 

Heres the code for the Form HTML

<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form method="post" name="myemailform" action="email.php">
<p>
<label for="fname">Enter First Name:</label>
<input type="text" name="fname"><br>
</p>
<p>
<label for="lname">Enter Last Name:</label>
<input type="text" name="lname"><br>
</p>
<p>
<label for="visitor_email">Enter Email:</label>
<input type="text" name="visitor_email"><br>
</p>
<p>
<label for="phonenumber">Enter Phone Number:</label>
<input type="text" name="phonenumber"><br>
</p>
<p>
Enter amount in Party:
<label for="adults">Adults:</label>
<input type="text" name="adults">
<label for="seniors">Seniors:</label>
<input type="text" name="seniors">
<label for="kids">Kids:</label>
<input type="text" name="kids">
<br></p>
<label for="times">Select Prefered Time</label>
<select name="times">
<option value="1 pm">1 PM</option>
<option value="1:30 pm">1:30 PM</option>
<option value="2 pm">2 PM</option>
<option value="2:30 pm">2:30 PM</option>
<option value="3 pm">3 PM</option>
<option value="3:30 pm">3:30 PM</option>
<option value="4 pm">4 PM</option>
<option value="4:30 pm">4:30 PM</option>
<option value="5 pm">5 PM</option>
<option value="5:30 pm">5:30 PM</option>
</select>
<br>
<br>
<label for="message">Special Requests:</label><br>
<textarea name="message" rows="10" cols="30">Insert request here</textarea>
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>

 

 

Heres the code for my PHP file

 

<?php
if (!isset($_POST["submit"]))
{
echo "error; you need to submit the form!";
}
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$visitor_email = $_POST["visitor_email"];
$phonenumber = $_POST["phonenumber"];
$adults = $_POST["adults"];
$seniors = $_POST["seniors"];
$kids = $_POST["kids"];
$times = $_POST["times"];
$message = $_POST["message"];
if(empty($lname)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
$email_from = "adam@radisson.com";
$email_subject ="New Form Submission From $lname \n";
$email_body ="You have received a new message from $fname $lname \n".
"Email Address: $visitor_email \n".
"Phone Number: $phonenumber \n".
"Amount in Party: $adults $seniors $kids \n".
"Here is the time requested: $times \n".
"Special Requests are: $message \n";
$to = "adam@radisson.com";
$headers = "from: $email_from \r\n";
mail ($to, $email_subject, $email_body, $headers);
?>

 

 

Thanks again

Link to comment
Share on other sites

It works for me? you have STILL have an error though, but! it will still send email.

1) As pointed out before

if (!isset($_POST["submit"]))
{
 echo "error; you need to submit the form!";
}

Will always be true and show error because

<input type="submit" value="Submit">

does not have name attribute with value of 'submit' as in

<input type="submit" name="submit" value="Submit">
Link to comment
Share on other sites

Sorry, when i sent the last message i didn't wait long enough for the email. it takes our email system where i work 5 to 10 minutes to receive emails that are sent out. i must have left the office before i sent it and was impatient to wait for it. lol

 

Anyways, everything worked and i'm going to look into the submit button a little bit more.

 

Appreciate all of the help!!!

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...