0

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 protected]”>Amanda</a> for pricing and details: 
        </legend>
            <input type="radio" name="packageSelect" value="Bronze Package" checked>&nbsp;Bronze
            <br />
            <br />
            <input type="radio" name="packageSelect" value="Silver Package">&nbsp;Silver
            <br />
            <br />
            <input type="radio" name="packageSelect" value="Gold Lunch Package">&nbsp;Gold&nbsp;(breakfast; exclusive sponsorship)
            <br />
            <br />
            <input type="radio" name="packageSelect" value="Gold Breakfast Package">&nbsp;Gold&nbsp;(lunch; exclusive sponsorship)
            <br />
            <br />
            <input type="radio" name="packageSelect" value="Gold Trade Show Package">&nbsp;Gold&nbsp;(trade&nbsp;show; exclusive sponsorship)
        </fieldset>
        <br />
    <button type="submit" name="submit">Submit</button>&nbsp;<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 protected]';
$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.\n".
              "Vendor Type: $vendorType\n".
            "Other Vendor Type: $other1\n".
            "Email Address: $email\n".
            "Phone Number: $phone\n".
            "Additional Questions: $questions\n".
            "Sponsorship Level: $packageSelect\n".

$to = '[email protected]';
$headers = "$email_from \r\n";
$headers .= "Reply-To: $email \r\n";

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?

6
  • 1
    Error logs? Why do you have smart quotes in source code? Commented Feb 19, 2014 at 18:43
  • Backticks spotted! Although I have seen some cases where it did work somehow, try to use either single quotes (') or double quotes ("). Commented Feb 19, 2014 at 18:47
  • They are, aren't they? Commented Feb 19, 2014 at 18:54
  • I've fixed all the backticks in the PHP (I think it was just a goof on my part when I submitted the question; I opened the code in a text editor to fix tabs, etc, and they got converted in places) and gone back to my original code to verify that they're actually ticks instead of smart quotes. Still, nothing is coming through when I try to submit the form. Commented Feb 19, 2014 at 18:57
  • This line if (empty($vendorName)||(empty($contactName)||(empty($vendorType)||(empty($email)||(empty($phone)||(empty($packageSelect)) should read as if (empty($vendorName)|| empty($contactName)|| empty($vendorType)|| empty($email)|| empty($phone)|| empty($packageSelect)) Commented Feb 19, 2014 at 19:03

6 Answers 6

1

(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\n". 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 protected]';

$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.\n".
              "Vendor Type: $vendorType\n".
            "Other Vendor Type: $other1\n".
            "Email Address: $email\n".
            "Phone Number: $phone\n".
            "Additional Questions: $questions\n".
            "Sponsorship Level: $packageSelect\n";

$to = "[email protected]";

$headers = 'From: ' . $email_from . "\r\n";

$headers .= "Reply-To: $email \r\n";

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@KrishR Thank you Krish, much obliged :)
@Fred-ii- That did it! The email submitted successfully. Now, for the redirect...what would I need to change in that last line to get it to show the 'thank-you.html' page I've specified?
You can try adding ob_start(); just under <?php and then using the redirect where I commented it out, and then comment out my echo "thanks"; @andrewdcato which I tested just now and worked.
See my edit/reload it. I placed it in there under <?php as a comment // ob_start(); @andrewdcato - Just remove the comment //
@Fred-ii- thanks so much. You've made my transition into figuring this PHP stuff out that much easier :)
1

You have syntax error in $email_from and $to. Need to use '(single quotes) or " (double quotes) instead of . Try this,

$email_from = '[email protected]';
           ...^                ^....

instead of

$email_from = ‘[email protected]’;

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;   
}

13 Comments

Ah yes, those dreadful smart/curly quotes. "Beautiful, yet deadly."
I've gone through and corrected all the backticks, but there's still nothing coming through when I try to submit the form.
By "nothing coming through", do you mean mail is 'sent' but no variables? Or "nothing" happens? @andrewdcato
And this line "Sponsorship Level: $packageSelect\n". should be "Sponsorship Level: $packageSelect\n"; there was a dot at the end of it, where it should have been a closing semi-colon. @andrewdcato
@Fred-ii- sorry for the confusion - no email is sent, and the redirect to the "thank you" page still isn't happening.
|
0
<button type="submit&" name="submit">Submit</button>&nbsp;<button type="reset" name="reset">Reset</button>

to

<button type="submit" name="submit">Submit</button>&nbsp;<button type="reset" name="reset">Reset</button>

Is the form submitting from frontend?

also in php code, the $email_from:

$email_from = ‘[email protected]’;

change it to:

$email_from = '[email protected]';

note the difference in single quotes. Same goes for $to:

$to = ‘[email protected]';

change it to:

$to = '[email protected]';

2 Comments

I've corrected all the errors you pointed out (ampersand and smart quotes in PHP), but still nothing. The form isn't submitting at all.
I see the required attributes, are you filling all the fields correctly?
0

Can you remove the "&" in <button type ="submit&">?

You also need to close the form using </form> after the last line

2 Comments

I've removed the ampersand from the button type declaration, but still get nothing when I try to submit. The missing form tag was a goof on my part when I inputted the HTML in the original question.
Did you enable the OpenSSL extension on PHP?
0

Why the & in type="submit&"? Also, there is no closing </form> tag.

3 Comments

I've removed the ampersand from the button type declaration, but still get nothing when I try to submit. The missing form tag was a goof on my part when I inputted the HTML in the original question.
You do have all the rquired html, head, body tags and closing tags?
Yes; my HTML validates without issue.
0

you forgot to put a semicolon after $email_body

please put one

$email_body = "";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.