通过PHP提交HTML表单

时间:2014-02-19 18:41:46

标签: php html forms post

尝试通过PHP向HTML电子邮件提交HTML5表单,并重定向到“谢谢!”成功提交后的页面。问题是,表单没有发送重定向没有发生。

这是我的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>&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>

这是我的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.\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@domain.net';
$headers = "$email_from \r\n";
$headers .= "Reply-To: $email \r\n";

mail($to,$email_subject,$email_body,$headers);
header('Location: thank-you.html');

?>

我不知道发生了什么,或者为什么这不起作用。有什么想法吗?

6 个答案:

答案 0 :(得分:1)

$email_from$to中存在语法错误。需要使用'(单引号)或"(双引号)而不是。试试这个,

$email_from = 'email@domain.net';
           ...^                ^....

而不是

$email_from = ‘email@domain.net’;

另外,在if条件下,您错过了大量)

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

答案 1 :(得分:1)

经过测试)试一试,这对我有用。

另外,你可能会收到一个错误,说“标题已经发送”,这对我来说很有用,所以我最后使用了一个回声,并评论了你的header("....进行测试。如果<?php之前有空格,则可能会出现错误消息。您可以尝试在正在打开的PHP标记下方使用ob_start();

您的empty条件)有太多,有些丢失/不在正确的位置。

另外,在"Sponsorship Level: $packageSelect\n".末尾有一个缺少的结束分号,其中有一个点。加上已添加的遗失From:

<?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.\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@domain.net";

$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";

?>

<强>脚注:

如果仍然没有“发送”,则更改:

<button type="submit" name="submit">Submit</button>

为:

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

如果您将header("...ob_start();一起使用,则不得使用其下方的echo。只是评论一下。

答案 2 :(得分:0)

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

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

表单是从前端提交的吗?

也在php代码中,$ email_from:

$email_from = ‘email@domain.net’;

将其更改为:

$email_from = 'email@domain.net';

注意单引号的差异。同样适用于$:

$to = ‘email@domain.net';

将其更改为:

$to = 'email@domain.net';

答案 3 :(得分:0)

您可以删除"&"

中的<button type ="submit&">?吗?

您还需要在最后一行

后使用</form>关闭表单

答案 4 :(得分:0)

为什么&中的type="submit&"?此外,没有关闭</form>标记。

答案 5 :(得分:0)

你忘了在$ email_body

之后加一个分号

请放一个

$email_body = "";