dongyu6276 2014-02-19 18:41 采纳率: 0%
浏览 44
已采纳

通过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>&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@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?

  • 写回答

6条回答 默认 最新

  • duancheng3042 2014-02-19 19:12
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳