通过PHP提交HTML表单
Attempting to submit an HTML5 form to email via PHP, with a redirect to a "Thank you!" page after a successful submission. Problem is, the form isn't sending and the redirect isn't happening.
Here's my HTML:
<form id="vendorInfo" action="process_form_vendor.php" method="post">
<label for="vendorName">Vendor Name:</label>
<br />
<input id="vendorName" name="vendorName" type="text" maxlength="30" required>
<br />
<label for="contactName">Contact Name:</label> <br />
<input id="contactName" name="contactName" type="text" maxlength="35" required>
<br />
<label for="vendorType">Organization Type:</label>
<br />
<select id="vendorType" name="vendorType">
<option value="carrier">
Insurance Carrier
</option>
<option value="tech_crm">
Technology/CRM Management
</option>
<option value="leadProvider">
Lead Provider
</option>
<option value="info_comm">
Information/Communication
</option>
<option value="other">
Other (please describe below)
</option>
</select>
<br />
<label for="other1">Other Organization Type:</label>
<br />
<input id="other1" name="other1" type="text" maxlength="25">
<br />
<label for="email">Email:</label>
<br />
<input id="email" name="email" type="email" maxlength="30" required>
<br />
<label for="phone">Phone:</label>
<br />
<input id="phone" name="phone" type="tel" maxlength="12" required placeholder="xxx-xxx-xxxx">
<br />
<label for="questions">Any questions or comments? Leave them here:</label>
<br />
<textarea id="questions" name="questions" rows="10" maxlength="300"></textarea>
<br />
<br />
<fieldset id="selectionBox">
<legend id="packageSelect">
The following sponsorship packages are available for the Sales Summit; contact <ahref="mailto:email@domain.com”>Amanda</a> for pricing and details:
</legend>
<input type="radio" name="packageSelect" value="Bronze Package" checked> Bronze
<br />
<br />
<input type="radio" name="packageSelect" value="Silver Package"> Silver
<br />
<br />
<input type="radio" name="packageSelect" value="Gold Lunch Package"> Gold (breakfast; exclusive sponsorship)
<br />
<br />
<input type="radio" name="packageSelect" value="Gold Breakfast Package"> Gold (lunch; exclusive sponsorship)
<br />
<br />
<input type="radio" name="packageSelect" value="Gold Trade Show Package"> Gold (trade show; exclusive sponsorship)
</fieldset>
<br />
<button type="submit" name="submit">Submit</button> <button type="reset" name="reset">Reset</button>
<br />
</form>
And here is my PHP:
<?php
if(!isset($_POST['submit']))
{
echo "error; you need to submit the form!";
}
$vendorName = $_POST['vendorName'];
$contactName = $_POST['contactName'];
$vendorType = $_POST['vendorType'];
$other1 = $_POST['other1'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$questions = $_POST['questions'];
$packageSelect = $_POST['packageSelect'];
if (empty($vendorName)||(empty($contactName)||(empty($vendorType)||(empty($email)||(empty($phone)||(empty($packageSelect)) {
echo "Vendor Name, Contact Name, Vendor Type, Email, Phone, and Package Selection are mandatory!";
exit;
}
$email_from = 'email@domain.net';
$email_subject = '2014 SMS Sales Summit - New Vendor Reservation Request';
$email_body = "You have received a new vendor reservation request for the 2014 SMS Sales Summit from $contactName at $vendorName.
".
"Vendor Type: $vendorType
".
"Other Vendor Type: $other1
".
"Email Address: $email
".
"Phone Number: $phone
".
"Additional Questions: $questions
".
"Sponsorship Level: $packageSelect
".
$to = 'email@domain.net';
$headers = "$email_from
";
$headers .= "Reply-To: $email
";
mail($to,$email_subject,$email_body,$headers);
header('Location: thank-you.html');
?>
I have no idea what's going on or why this isn't working. Any ideas?
(Tested) Give this a try, it worked for me.
Plus, you may get an error saying "headers already sent", which did for me, so I used an echo at the end and I commented your header("....
to test with. If you have a space before <?php
this could cause the error message to appear. You can try using ob_start();
just below your opening PHP tag.
Your empty
conditionals )
had some too many, and some missing/not at the right spot.
Plus, a missing closing semi-colon at the end of "Sponsorship Level: $packageSelect
".
where there was a dot. Plus a missing From:
which has been added.
<?php
// uncomment line below to use with header redirect
// ob_start();
if(!isset($_POST['submit']))
{
echo "error you need to submit the form!";
}
$vendorName = $_POST['vendorName'];
$contactName = $_POST['contactName'];
$vendorType = $_POST['vendorType'];
$other1 = $_POST['other1'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$questions = $_POST['questions'];
$packageSelect = $_POST['packageSelect'];
if (empty($vendorName)|| empty($contactName)|| empty($vendorType)|| empty($email)|| empty($phone)|| empty($packageSelect)){
echo "Vendor Name, Contact Name, Vendor Type, Email, Phone, and Package Selection are mandatory!";
exit;
}
$email_from = 'email@domain.net';
$email_subject = '2014 SMS Sales Summit - New Vendor Reservation Request';
$email_body = "You have received a new vendor reservation request for the 2014 SMS Sales Summit from $contactName at $vendorName.
".
"Vendor Type: $vendorType
".
"Other Vendor Type: $other1
".
"Email Address: $email
".
"Phone Number: $phone
".
"Additional Questions: $questions
".
"Sponsorship Level: $packageSelect
";
$to = "email@domain.net";
$headers = 'From: ' . $email_from . "
";
$headers .= "Reply-To: $email
";
mail($to,$email_subject,$email_body,$headers);
// header('Location: thank-you.html');
echo "thanks";
?>
Footnotes:
If it still does not "send", then change:
<button type="submit" name="submit">Submit</button>
to:
<input type="submit" name="submit" value="Submit">
If you use the header("...
along with the ob_start();
you must not use the echo
below it. Just comment that out.
<button type="submit&" name="submit">Submit</button> <button type="reset" name="reset">Reset</button>
to
<button type="submit" name="submit">Submit</button> <button type="reset" name="reset">Reset</button>
Is the form submitting from frontend?
also in php code, the $email_from:
$email_from = ‘email@domain.net’;
change it to:
$email_from = 'email@domain.net';
note the difference in single quotes. Same goes for $to:
$to = ‘email@domain.net';
change it to:
$to = 'email@domain.net';
Can you remove the "&"
in <button type ="submit&">?
You also need to close the form using </form>
after the last line
Why the &
in type="submit&"
? Also, there is no closing </form>
tag.
You have syntax error in $email_from
and $to
. Need to use '
(single quotes) or "
(double quotes) instead of ‘
. Try this,
$email_from = 'email@domain.net';
...^ ^....
instead of
$email_from = ‘email@domain.net’;
Also, in your if
condition you have missed to add lots of )
if (empty($vendorName)||
empty($contactName)||
empty($vendorType)||
empty($email)||
empty($phone)||
empty($packageSelect) ) {
echo "Vendor Name, Contact Name, Vendor Type, Email, Phone, and Package Selection are mandatory!";
exit;
}
you forgot to put a semicolon after $email_body
please put one
$email_body = "";