Sending Form Data with Attachment - php

Ok, I've gotten the attachment working in the email and it's being sent correctly thanks to this bit that I found here
What this is getting me is an email with an attachment, but none of the other form data is being injected into the email. I'm sure it's a problem with the syntax that I'm using, but I con't for the life of me figure out what I'm doing wrong. I've looked at about a billion other form examples that I've found here (and other less helpful nooks and crannies of the web)
Also, before the downvotes pour in, I realize I haven't done any sort of validation yet, and I'm not forwarding the user to a success URL after the message has been sent. I'm just trying to get the script working the way I think it should before I start going nuts over validating it.
Here is the html for my form:
<form class="pure-form pure-form-stacked" enctype="multipart/form-data"
method="POST" action="mail.php">
<fieldset>
<legend>About You</legend>
<div class="pure-g">
<div class="pure-u-1 pure-u-md-1-3">
<label for="firstname">First Name</label>
<input id="firstname" name="firstname" type="text" required>
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="lastname">Last Name</label>
<input id="lastname" name="lastname" type="text" required>
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="email">E-Mail</label>
<input id="email" name="email" type="email" required>
</div>
<div class="pure-u-1 pure-u-md-2-3">
<label for="street">Street Address</label>
<input id="street" name="street" type="text">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="city">City</label>
<input id="city" name="city" type="text">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="zip">Zip</label>
<input id="zip" name="zip" type="text">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="phone">Phone</label>
<input id="phone" name="phone" type="tel" placeholder="xxx-xxx-xxxx" required>
</div>
</div>
<legend>About Your Company</legend>
<div class="pure-g" >
<div class="pure-u-1 pure-u-md-1-3">
<label for="companyname">Company Name</label>
<input id="companyname" name="companyname" type="text" required>
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="companysize"># of Employees?</label>
<input id="companysize" name="companysize" type="number" required>
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="companywebsite">Website</label>
<input id="companywebsite" name="companywebsite" type="url" required placeholder="eg www.mysite.com">
</div>
<div class="pure-u-1 pure-u-md-1-1">
<label for="companylogo">Upload your Logo</label>
<input id="companylogo" type="file" name="attachment[]" >
</div>
</div>
<button type="submit" class="pure-button pure-button-primary">Submit</button>
</fieldset>
And here is the php from 'mail.php'
<?php
if(isset($_POST['submit']))
{ //check form inputs
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$street = $_POST['street'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$companyname = $_POST['companyname'];
$companysize = $_POST['companysize'];
$companywebsite = $_POST['companywebsite'];
}
if( $_POST || $_FILES )
{
// email fields: to, from, subject, and so on
// Here
$from = "host#notareal.com";
$to = "setmeup#notareal.com";
$subject = "Setup Request from Free Setup Form";
$message = $firstname.$lastname.$email.$street.$city.$zip.$phone.$companyname.$companysize.$companywebsite;
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
fixFilesArray($_FILES['attachment']);
foreach ($_FILES['attachment'] as $position => $file)
{
// should output array with indices name, type, tmp_name, error, size
$message .= "--{$mime_boundary}\n";
$fp = #fopen($file['tmp_name'],"rb");
$data = #fread($fp,filesize($file['tmp_name']));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".$file['name']."\"\n"."Content-Description: ".$file['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$file['name']."\";size=".$file['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
$ok = #mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return 1; } else { return 0; }
}
//This function will correct file array from $_FILES[[file][position]] to $_FILES[[position][file]] .. Very important
function fixFilesArray(&$files)
{
$names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);
foreach ($files as $key => $part) {
// only deal with valid keys and multiple files
$key = (string) $key;
if (isset($names[$key]) && is_array($part)) {
foreach ($part as $position => $value) {
$files[$position][$key] = $value;
}
// remove old key reference
unset($files[$key]);
}
}
}
?>

That's because $_POST["submit"] is never set - your button doesn't have a name.

Related

Can't get HTML/PHP form to work after adding image attachment option

I just added an attachment to my PHP form. It was working perfectly fine before adding the attachment, but now when submitting the form it goes to a browser error page / unable to complete request.
<?php
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => 'captchasecret',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
print "<meta http-equiv=\"refresh\" content=\"0;URL=captcha-failed.html\">";
} else {
// If CAPTCHA is successfully completed...
// Paste mail function or whatever else you want to happen here!
//echo '<br><p>CAPTCHA was completed successfully!</p><br>';
//}
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: {$Email}>" . "\r\n";
$EmailFrom = "mywebsite.com";
$EmailTo = "myemail.com";
$Subject = "TV Form";
$Name = "";
$Businessname="";
$Tel="";
$Email="mail#jarbcom.com";
$Business="";
$Locations="";
$Units="";
$DishCustomer="";
$Zip="";
$message="";
if (isset($_POST['businessname'])) {
$BusinessName = Trim(stripslashes($_POST['businessname']));
}
if (isset($_POST['name'])) {
$Name = Trim(stripslashes($_POST['name']));
}
if (isset($_POST['phone'])) {
$Tel = Trim(stripslashes($_POST['phone']));
}
if (isset($_POST['email'])) {
$Email = Trim(stripslashes($_POST['email']));
}
if (isset($_POST['business'])) {
$Business = Trim(stripslashes($_POST['business']));
}
if (isset($_POST['locations'])) {
$Locations = Trim(stripslashes($_POST['locations']));
}
if (isset($_POST['units'])) {
$Units = Trim(stripslashes($_POST['units']));
}
if (isset($_POST['dishcustomer'])) {
$DishCustomer = Trim(stripslashes($_POST['dishcustomer']));
}
if (isset($_POST['zip'])) {
$Zip = Trim(stripslashes($_POST['zip']));
}
if (isset($_POST['message'])) {
$message = Trim(stripslashes($_POST['message']));
}
if (isset($_POST['address'])) {
$Address = Trim(stripslashes($_POST['address']));
}
if (isset($_POST['tvsize'])) {
$Tvsize = Trim(stripslashes($_POST['tvsize']));
}
if (isset($_POST['tvnum'])) {
$Tvnum = Trim(stripslashes($_POST['tvnum']));
}
if (isset($_POST['boxes'])) {
$Boxes = Trim(stripslashes($_POST['boxes']));
}
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=404.html\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "<br />\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "<br />\n";
$Body .= "Zip Code: ";
$Body .= $Zip;
$Body .= "<br />\n";
$Body .= "Phone: ";
$Body .= $Tel;
$Body .= "<br />\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "<br />\n";
$Body .= "Tv Size: ";
$Body .= $Tvsize;
$Body .= "<br />\n";
$Body .= "Number of TVs: ";
$Body .= $Tvnum;
$Body .= "<br />\n";
$Body .= "Where they want the boxes: ";
$Body .= $Boxes;
$Body .= "<br />\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "<br />\n";
$Body .= "File: " . clean_string($attachment) . "\n\n";
// send email
$success = mail($EmailTo, $Subject, $Body, $headers);
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thank-you.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=404.html\">";
}
}
?>
Here is the parts of my HTML form that matter (no point in making you read all the fields)
I do have multipart/form-data
<form action="phpformcontact.php" method="POST" enctype="multipart/form-data">
And this is the field for my attachment I added
<label for="tvimage" class="sr-only control-label">Image to upload</label>
<input placeholder="Upload Image" type="file" name="attachment" id="file" class="form-control input-lg"><span class="help-block text-danger"></span>
The form works perfectly fine before adding the attachment code. Any help would be appreciated. Also, is there an easy way to add a filter to deny the attachment if it's not png/jpeg/pdf/etc. Otherwise isn't it really easy for someone to upload a virus to your server?
Thank you in advanced for your help.
EDIT: By request, here is the entire HTML form
<form action="phpformcontact.php" method="POST" enctype="multipart/form-data">
<div class="col-md-4">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="name" class="sr-only control-label">Name*</label>
<input id="name" name="name" type="text" placeholder="Full Name&#42" required="" data-validation-required-message="Please enter your full name" class="form-control input-lg"><span class="help-block text-danger"></span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="address" class="sr-only control-label">Address*</label>
<input id="address" name="address" type="text" placeholder="Address&#42" required="" data-validation-required-message="Please enter your address" class="form-control input-lg"><span class="help-block text-danger"></span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="zip" class="sr-only control-label">Zip Code</label>
<input id="zip" name="zip" type="text" placeholder="Business Zip Code&#42" required="" data-validation-required-message="Please enter your business zip code" class="form-control input-lg"><span class="help-block text-danger"></span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="phone" class="sr-only control-label">Phone*</label>
<input id="phone" name="phone" type="tel" placeholder="Phone&#42" required="" data-validation-required-message="Please enter phone number" class="form-control input-lg"><span class="help-block text-danger"></span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="email" class="sr-only control-label">Email*</label>
<input id="email" name="email" type="email" placeholder="Email&#42" required="" data-validation-required-message="Please enter email" class="form-control input-lg"><span class="help-block text-danger"></span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="tvsize" class="sr-only control-label">TV Size*</label>
<input id="tvsize" name="tvsize" type="text" placeholder="TV Size&#42" required="" data-validation-required-message="Please enter the size of the TV(s)" class="form-control input-lg"><span class="help-block text-danger"></span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="tvnum" class="sr-only control-label">Number of TVs*</label>
<input id="tvnum" name="tvnum" type="text" placeholder="Numbers of TVs&#42" required="" data-validation-required-message="Please enter the number of TVs" class="form-control input-lg"><span class="help-block text-danger"></span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="boxes" class="sr-only control-label">Where do you want the boxes?</label>
<!-- <input id="business" type="text" placeholder="Type of Business" required="" data-validation-required-message="Please enter your type of business" class="form-control input-lg"><span class="help-block text-danger"></span> -->
<select name="boxes" class="form-control input-lg"><span class="help-block text-danger">
<option value="">Where do you want the boxes?</option>
<option value="same-room" onclick="changeValue('Same Room');">Same Room</option>
<option value="different-location" onclick="changeValue('Different Location');">Different Location</option>
<option value="idk" onclick="changeValue('Don't Know');">Don't Know</option></select></div></div></div>
Image to upload
<div class="col-md-12">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="message" class="sr-only control-label">Details</label>
<textarea name="message" id="message" rows="2" placeholder="Any Other Details" class="form-control input-lg"></textarea><span class="help-block text-danger"></span>
</div>
</div>
<div id="success"></div>
<button type="submit" class="btn btn-dark btn-lg text-center btn-danger">Send</button>
</div>
</form>

Return message status in page after send mail with php

I try to show a message in html page, after send an e-mail.
I don't want to use javascript with an alert, just a simple message after send button.
I made a contact php page with this code:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_telefon = $_POST['cf_telefon'];
$field_message = $_POST['cf_message'];
$mail_to = 'diaconu.eduardstefan#gmail.com';;
$from = 'Mesaj nou de la:'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Telefon: '.$field_telefon."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $from, $body_message, $headers);
?>
And in html page, i insert a contact form with this code:
<form action="contact.php" method="post">
<div class="form-group">
<label for="name">
Nume
</label>
<input type="text" name="cf_name" placeholder="" id="name" class="form-control" required="true">
</div>
<div class="form-group">
<label for="email">
Email
</label>
<input type="text" name="cf_email" placeholder="" id="email" class="form-control" required="true">
</div>
<div class="form-group">
<label for="phone">
Telefon
</label>
<input type="text" name="cf_telefon" id="phone" class="form-control">
</div>
<div class="form-group">
<label for="phone">
Mesaj
</label>
<textarea name="cf_message" placeholder="" rows="5" class="form-control" required="true">
</textarea>
</div>
<input class="btn btn-info" type="submit" value="Trimite" >
<?php if($send_mail) {
if($mail_status){
print "succes";
exit();
}
else {
print "eroare";
}
}
?>
</form>
Email was successfully sent, but the message isn't show. After press send button, contact form return a blank page. I want to return the same page with message. (ater refresh)
Maybe exist another way to do that? With get or something like that?
Thanks for help!
Place your PHP in the top of the page of where your contact form is.
then change: <form action="contact.php" method="post">
to
<form action="" method="post">
you also need to add name="submit" to your submit button field and then wrap:
if (isset($_POST['submit'])) {
// code
}
Also if you are trying to make sure people fill in all fields, you should use
if (!empty($var))
{
// code
} else
echo "Fill in this field please";
}
required fields can be easily bypassed by just using required within the html side.
EDIT: once all conditions are met you can simply add echo "thanks" or whatever message you want to beneath the mail() field for a success message which will output to the page above the form.
if($_POST){
// send mail code here
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_telefon = $_POST['cf_telefon'];
$field_message = $_POST['cf_message'];
$mail_to = 'diaconu.eduardstefan#gmail.com';;
$from = 'Mesaj nou de la:'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Telefon: '.$field_telefon."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $from, $body_message, $headers);
if($mail_status){
echo 'mail send';
}else{
echo 'mail not send';
}
}
//place html code here below the php code
<form action="" method="post">
<div class="form-group">
<label for="name">
Nume
</label>
<input type="text" name="cf_name" placeholder="" id="name" class="form-control" required="true">
</div>
<div class="form-group">
<label for="email">
Email
</label>
<input type="text" name="cf_email" placeholder="" id="email" class="form-control" required="true">
</div>
<div class="form-group">
<label for="phone">
Telefon
</label>
<input type="text" name="cf_telefon" id="phone" class="form-control">
</div>
<div class="form-group">
<label for="phone">
Mesaj
</label>
<textarea name="cf_message" placeholder="" rows="5" class="form-control" required="true">
</textarea>
</div>
<input class="btn btn-info" type="submit" value="Trimite" >
</form>

PHP Contact form empty body (bootstrap)

I am having trouble with my contact form (from bootstrap). the php code as well as the html code are as seen below. Whenever I try the contact form, the body will be empty. Am I missing anything?
This is the html code:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your Last Name">
<label>Email Address</label>
<input type="text" class="input-block-level" required="required" placeholder="Your email address">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required="required" class="input-block-level" rows="8"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
<p> </p>
</form>
This is the PHP code:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'mail#luckystarmaids.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Your name and email form elements don't have name attributes. Without them, they won't be posted to the form's action1, 2.
Add names to your inputs:
<input type="text" name="first_name" class="input-block-level" required="required" placeholder="Your Last Name">
Also you will need to handle first and last name in your PHP code, at the moment you're only looking for name.
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input name=firstname type="text" class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input name=lastname type="text" class="input-block-level" required="required" placeholder="Your Last Name">
<label>Email Address</label>
<input name=email type="text" class="input-block-level" required="required" placeholder="Your email address">
<label>Subject</label>
<input name=subject type="text" class="input-block-level" required="required" placeholder="Subject">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required="required" class="input-block-level" rows="8"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
<p> </p>
</form>
Try also to handle the data on the server. Check at least if the values are send:
<?php
if ( isset( $_POST['firstname'] )
&& isset( $_POST['email'] )
&& isset( $_POST['subject'] )
&& isset( $_POST['message'] ) )
{
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'mail#luckystarmaids.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $s
ubject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
if ($success)
{
echo json_encode(array(
'success'=> true,
'message'=>'Email sent, you recieve an email at ' . $email
));
}
}
else
{
echo json_encode(array(
'success' => false,
'message' => 'An error has occured, please try again.'
));
}
?>

How do I stop the Phantom Emails from my PHP code?

I'm currently using an html5 website with a contact form and a php action to receive contact emails. I've tried a few different codes in my php but non have stopped the blank emails from coming. I do have google analytics enables on my code but have blocked the crawler through robot.txt. Here is my code.
PHP CODE
<?php
foreach ($_GET as $Field=>$Value) {
if($Value != ''){
$body .= "$Field: $Value\n";
}
}
$name = trim(htmlentities($_GET['name'],ENT_QUOTES,'utf-8'));
$email = trim(htmlentities($_GET['email'],ENT_QUOTES,'utf-8'));
$phone = trim(htmlentities($_GET['phone'],ENT_QUOTES,'utf-8'));
$messages = trim(htmlentities($_REQUEST['messages'],ENT_QUOTES,'utf-8'));
if (strlen($name) == 0 )
{
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
if (strlen($email) == 0 )
{
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
$to = "junior.8791#gmail.com";
$message = "Name: ".$name;
$message.="\n\nEmail: ".$email;
$message.="\n\nPhone: ".$phone;
$message .= "\n\nMessage: ".$messages;
$headers = "From: $email";
$headers .="\nReply-To: $email";
$success = mail($to, $subject, $message, $headers);
if ($success) {
echo "<script>window.location = 'http://www.mason372.org/thankyou.html'</script>";
} else {
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
?>
CONTACT FORM
<form action="email.php" method="post" id="form">
<div class="5grid">
<div class="row">
<div class="6u">
<input type="text" class="name" name="name" id="name" placeholder="Name" value="" aria-describedby="name-format" required aria-required=”true” pattern="[A-Za-z-0-9]+\s[A-Za-z-'0-9]+" title="e.g.'John Doe'" required="" />
</div>
<div class="6u">
<input type="email" class="email" name="email" id="email" placeholder="Email" required="" />
</div>
</div>
<div class="row">
<div class="12u">
<input type="tel" class="tel" name="phone" id="phone" name="phone" type="text" placeholder="Phone Number" pattern="(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}" required />
</div>
</div>
<div class="row">
<div class="12u">
<textarea name="messages" id="messages" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input type="submit" class="button" value="Send Message">
<input type="reset" class="button button-alt" value="Clear Form">

Can't get my php form working, sends mail but doesn't run code on redirect page

This question gets asked all the time, but I can't figure out why mine isn't working. I have a form that redirects to itself. If PHP decides it is submitted, there is a success/failure message and it displays the user input as the default value and disables the fields: using phpinfo I can see that the form is being submitted, but this first conditional doesn't work. I've tried a couple of versions, but no luck. It's weird because it sends the email
Specifically, the result and disable functions don't display their code after the form has been sent.
<?php
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}
function result(){
if($sent) echo $result;
}
function disable($field){
if($sent){
if($field != null){
$ret .= $field . '", disabled, placeholder!="';
}
$ret .= '", disabled, placeholder!="';
echo $ret;
}
}
function option($item){
$ret = "<option>";
if($sent){
if($eventType == $item){
$ret = "<option selected>";
}
}
$ret .= $item . "</option>";
echo $ret;
}
if(isset($_POST['name'])){
$sent = TRUE;
$result = null;
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$eventDate = $_POST['eventDate'];
$eventTime = $_POST['eventTime'];
$eventLength = $_POST['eventLength'];
$eventLocation = $_POST['eventLocation'];
$eventType = $_POST['eventType'];
$message = $_POST['message'];
$recipient = "";
$subject = " Form Submission";
$mailheader = "From: \r\n";
$formcontents = "You received this e-mail message through your website: \n\n";
$formcontents .= "Name: " . clean($name) . "\r\n";
$formcontents .= "Phone: " . clean($phone) . "\r\n";
$formcontents .= "Email: " . clean($email) . "\r\n";
$formcontents .= "Event Date: " . clean($eventDate) . "\r\n";
$formcontents .= "Event Time: " . clean($eventTime) . "\r\n";
$formcontents .= "Event Length: " . clean($eventLength) . "\r\n";
$formcontents .= "Event Location: " . clean($eventLocation) . "\r\n";
$formcontents .= "Event Type: " . clean($eventType) . "\r\n";
$formcontents .= "Message: " . clean($message) . "\r\n";
$formcontents .= "\r\n";
$formcontents .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
$formcontents .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n";
// Send mail
if(mail($recipient, $subject, $formcontents, $mailheader)){;
$result = '<h3 class="alert alert-success"> Thank you, your form was successfully sent and I will contact you shortly.</h3>';
} else {
$result = '<h3 class="alert alert-error"> Your mail could not be sent at this time.</h3>';
}
}
?>
<form action="contact.php" method="POST" class="form-horizontal span4">
<fieldset>
<legend>
<h2>Or send me a message. </h2>
</legend>
<p class="help-block">None of the fields are required, but the more information I have about your event, the more detailed I can be in my response.</p>
<legend class="help-block">Your Details</legend>
<div class="control-group">
<label for="name" class="control-label">Your Name</label>
<div class="controls">
<input id="name" type="text" name="name" placeholder="<?php disable($name); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="phone" class="control-label">Your Contact Number</label>
<div class="controls">
<input id="phone" type="tel" name="phone" placeholder="<?php disable($phone); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="email" class="control-label">Your Email</label>
<div class="controls">
<input id="email" type="email" name="email" placeholder="<?php disable($email); ?>" class="input-xlarge"/>
</div>
</div>
<legend class="help-block">Your Event </legend>
<div class="control-group">
<label for="eventDate" class="control-label">Your Event's Date</label>
<div class="controls">
<input id="eventDate" type="date" name="eventDate" placeholder="<?php disable($eventDate); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventTime" class="control-label">Your Event's Start Time</label>
<div class="controls">
<input id="eventTime" type="time" name="eventTime" placeholder="<?php disable($eventTime); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventLength" class="control-label">Your Event's Length</label>
<div class="controls">
<input id="eventLength" type="text" name="eventLength" placeholder="<?php disable($eventLength); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventLocation" class="control-label">Your Event's Location</label>
<div class="controls">
<input id="eventLocation" type="text" name="eventLocation" placeholder="<?php disable($eventLocation); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventType" class="control-label">What Kind of Event</label>
<div class="controls">
<select id="eventType" name="eventType" placeholder="<?php disable($eventType); ?>"><?php option("Charity Event"); option("Expo/Trade Show"); option("Personal Event"); option("Other"); ?></select>
</div>
</div>
<div class="control-group">
<label for="message" class="control-label">Other comments or the best time to reach you.</label>
<div class="controls">
<textarea id="message" name="message" rows="10" placeholder="<?php disable($message); ?>" class="input-xxlarge"></textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" name="submit" placeholder="<?php disable(null); ?>" class="btn btn-primary">Send Message</button>
</div>
</fieldset>
</form>
You have to import your global variables into function scope, like:
function result(){
global $sent, $result;
if($sent) echo $result;
}
..in functions disable() and option(), too.
if(isset($_POST['name'])){
should be
if(isset($_POST['submit'])){

Categories