0

我正在尝试关闭我的联系页面的 PHP 代码(没有下拉选择,所有文本字段),效果很好。但是当我尝试让它与下拉选择一起工作时,它不起作用,它只是不断给我错误。

这是我的表单的 HTML 代码:

    <form action="order.php" method="post" id="contactform">


        <ul id="questions">




        <li>                <p>category</p>             <select data-placeholder="Select a category" name="category" id="selZOX" style="display: none;" class="chzn-done">                  <option></option><option value="Snack &amp; Quick Meals" data-connection="snackSelect">Snack &amp; Quick Meals</option><option value="Wraps" data-connection="wrapSelect">Wraps</option><option value="Breads" data-connection="breadSelect">Breads</option><option value="Chicken Entrees" data-connection="chickenSelect">Chicken Entrees</option><option value="Lahawajawaab Mutton" data-connection="laSelect">Lahawajawaab Mutton</option><option value="Rice Dishes" data-connection="riceSelect">Rice Dishes</option><option value="Vegetarian" data-connection="vegSelect">Vegetarian</option>              </select><div id="selZOX_chzn" class="chzn-container chzn-container-single" style="width: 300px;"><a href="javascript:void(0)" class="chzn-single" tabindex="0"><span>Snack &amp; Quick Meals</span><div><b></b></div></a><div class="chzn-drop" style="left: -9000px; width: 298px; top: 27px;"><div class="chzn-search"><input type="text" autocomplete="off" style="width: 263px;" tabindex="-1"></div><ul class="chzn-results"><li id="selZOX_chzn_o_1" class="active-result result-selected" style="">Snack &amp; Quick Meals</li><li id="selZOX_chzn_o_2" class="active-result" style="">Wraps</li><li id="selZOX_chzn_o_3" class="active-result" style="">Breads</li><li id="selZOX_chzn_o_4" class="active-result" style="">Chicken Entrees</li><li id="selZOX_chzn_o_5" class="active-result" style="">Lahawajawaab Mutton</li><li id="selZOX_chzn_o_6" class="active-result" style="">Rice Dishes</li><li id="selZOX_chzn_o_7" class="active-result" style="">Vegetarian</li></ul></div></div>                <span class="divider"></span>           </li><li>               <p>meal</p>             <select data-placeholder="Select a meal" name="meal" id="selJ3O" style="display: none;" class="chzn-done">                  <option></option><option value="Samosa - $2.99" data-connection="spiceSelect">Samosa - $2.99</option><option value="Aloo Tiki - $2.99" data-connection="spiceSelect">Aloo Tiki - $2.99</option><option value="Chana Samosa - $4.99" data-connection="spiceSelect">Chana Samosa - $4.99</option><option value="Tiki Chaat - $4.99" data-connection="spiceSelect">Tiki Chaat - $4.99</option><option value="Choley Bhaturey - $7.99" data-connection="spiceSelect">Choley Bhaturey - $7.99</option>               </select><div id="selJ3O_chzn" class="chzn-container chzn-container-single" style="width: 300px;"><a href="javascript:void(0)" class="chzn-single" tabindex="0"><span>Aloo Tiki - $2.99</span><div><b></b></div></a><div class="chzn-drop" style="left: -9000px; width: 298px; top: 27px;"><div class="chzn-search"><input type="text" autocomplete="off" style="width: 263px;" tabindex="-1"></div><ul class="chzn-results"><li id="selJ3O_chzn_o_1" class="active-result" style="">Samosa - $2.99</li><li id="selJ3O_chzn_o_2" class="active-result result-selected" style="">Aloo Tiki - $2.99</li><li id="selJ3O_chzn_o_3" class="active-result" style="">Chana Samosa - $4.99</li><li id="selJ3O_chzn_o_4" class="active-result" style="">Tiki Chaat - $4.99</li><li id="selJ3O_chzn_o_5" class="active-result" style="">Choley Bhaturey - $7.99</li></ul></div></div>             <span class="divider"></span>           </li><li>               <p>spicy</p>                <select data-placeholder="Select an option" name="spicy" id="selJME" style="display: none;" class="chzn-done">                  <option></option><option value="Very Spicy" data-connection="deliverSelect">Very Spicy</option><option value="Spicy" data-connection="deliverSelect">Spicy</option><option value="Medium" data-connection="deliverSelect">Medium</option><option value="Mild" data-connection="deliverSelect">Mild</option><option value="Not Spicy" data-connection="deliverSelect">Not Spicy</option>             </select><div id="selJME_chzn" class="chzn-container chzn-container-single" style="width: 300px;"><a href="javascript:void(0)" class="chzn-single" tabindex="0"><span>Medium</span><div><b></b></div></a><div class="chzn-drop" style="left: -9000px; width: 298px; top: 27px;"><div class="chzn-search"><input type="text" autocomplete="off" style="width: 263px;" tabindex="-1"></div><ul class="chzn-results"><li id="selJME_chzn_o_1" class="active-result" style="">Very Spicy</li><li id="selJME_chzn_o_2" class="active-result" style="">Spicy</li><li id="selJME_chzn_o_3" class="active-result result-selected" style="">Medium</li><li id="selJME_chzn_o_4" class="active-result" style="">Mild</li><li id="selJME_chzn_o_5" class="active-result" style="">Not Spicy</li></ul></div></div>              <span class="divider"></span>           </li><li>               <p>delivery</p>             <select data-placeholder="Select an option" name="delivery" id="selXE9" style="display: none;" class="chzn-done">                   <option></option><option value="Yes">Yes</option><option value="No">No</option>             </select><div id="selXE9_chzn" class="chzn-container chzn-container-single" style="width: 300px;"><a href="javascript:void(0)" class="chzn-single" tabindex="0"><span>No</span><div><b></b></div></a><div class="chzn-drop" style="left: -9000px; width: 298px; top: 27px;"><div class="chzn-search"><input type="text" autocomplete="off" style="width: 263px;" tabindex="-1"></div><ul class="chzn-results"><li id="selXE9_chzn_o_1" class="active-result" style="">Yes</li><li id="selXE9_chzn_o_2" class="active-result result-selected" style="">No</li></ul></div></div>              <span class="divider"></span>           </li></ul>

        <div id="preloader" style="display: none;"></div>
        <div class="clear"></div>

         <ul id="order2">
            <li>
                        <p>Any Additional Comments</p>

              <textarea id="message" name="message" rows="6" cols="50"></textarea>
            </li>
            </ul>

            <span class="divider"></span>

            <div class="clear"></div>

        <ul id="order">
            <li>
            <p>Name</p>
              <input id="name" name="name" class="text">
            </li>
            <li class="right">
            <p>Email Address</p>
              <input id="email" name="email" class="text">
            </li>
            <li>
            <p>Phone Number</p>
              <input id="phone" name="phone" class="text">
            </li>
            <li class="right">
            <p>Address</p>
              <input id="address" name="address" class="text">
            </li>
            <li>
            <p>City</p>
              <input id="city" name="city" class="text">
            </li>
            <li class="right">
            <p>Zip Code</p>
              <input id="zip" name="zip" class="zip">
            </li>

          </ul>
        <div class="clear"></div>




<li class="buttons">
              <input type="image" name="imageField" id="imageField" src="images/send.gif" class="send">
              <div class="clr"></div>
            </li>
          </form>

这是我的 PHP 代码:

<?php
if(isset($_POST['email'])) {

$email_to = "email@myemail.com";
$email_subject = "subject";


function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted: <br />";
    echo $error."<br />";
    echo "Please fix these errors.<br />";
    die();
}

// validation expected data exists
if(!isset($_POST['name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['phone']) ||
    !isset($_POST['address']) ||
    !isset($_POST['city']) ||
    !isset($_POST['zip']) ||
    !isset($_POST['category']) ||
    !isset($_POST['meal']) ||
    !isset($_POST['spicy']) ||
    !isset($_POST['delivery']) ||
    !isset($_POST['message'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$phone = $_POST['address']; // not required
$phone = $_POST['city']; // not required
$phone = $_POST['zip']; // not required
$category = $_POST['category']; // not required
$meal = $_POST['meal']; // not required
$spicy = $_POST['spicy']; // not required
$delivery = $_POST['delivery']; // not required
$message = $_POST['message']; // not required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
        $string_exp = "/^[A-Za-z .'-]+$/";
          if(!preg_match($string_exp,$name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }


  if(strlen($error_message) > 0) {
    died($error_message);
  }
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "Zip: ".clean_string($zip)."\n";
$email_message .= "Meal Category: ".clean_string($category)."\n";
$email_message .= "Meal: ".clean_string($meal)."\n";
$email_message .= "How Spicy: ".clean_string($spicy)."\n";
$email_message .= "Delivery: ".clean_string($delivery)."\n";
$email_message .= "Other Details: ".clean_string($message)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 

    echo 'Thank you, your message has been sent! <br /> We will review your submission and contact you as soon as possible.'; 

}
?>

和我的剧本:

<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function(){
    $('#contactform').submit(function(){                  
        var action = $(this).attr('action');
        $.post(action, { 
            category: $('#category').val(),
            meal: $('#meal').val(),
            spicy: $('#spicy').val(),
            delivery: $('#delivery').val(),
            email: $('#email').val(),
            phone: $('#phone').val(),
            address: $('#address').val(),
            city: $('#city').val(),
            zip: $('#zip').val(),
            message: $('#message').val()
        },
            function(data){
                $('#contactform #submit').attr('disabled','');
                $('.response').remove();
                $('#contactform').before('<p class="response">'+data+'</p>');
                $('.response').slideDown();
                if(data=='Thank you, your message has been sent! <br /> We will review your submission and contact you as soon as possible.') $('#contactform').slideUp();
            }
        ); 
        return false;
    });
});
// ]]>
</script>

任何帮助将不胜感激。谢谢你!

4

1 回答 1

0

我发现您的代码有两件事不正确。

1)您的标题格式不正确,请使用以下内容:

$headers = "From: $email_from" . "\r\n" .
"Reply-To: $email_from" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

在测试和使用您的标头时,我从未收到过电子邮件。

2)name="category[]"除非您打算让人们选择多个选项,否则您不需要括号。在这种情况下,我建议您使用复选框。

使用下面的 HTML 表单和 PHP,能够成功接收category选中的电子邮件。

HTML 表单(我放在一起测试)

注意style="display: none;":我出于测试目的删除并使用了我自己的 jQuery JS src

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function(){
    $('#contactform').submit(function(){                  
        var action = $(this).attr('action');
        $.post(action, { 
            category: $('#category').val(),
            meal: $('#meal').val(),
            spicy: $('#spicy').val(),
            delivery: $('#delivery').val(),
            email: $('#email').val(),
            phone: $('#phone').val(),
            address: $('#address').val(),
            city: $('#city').val(),
            zip: $('#zip').val(),
            message: $('#message').val()
        },
            function(data){
                $('#contactform #submit').attr('disabled','');
                $('.response').remove();
                $('#contactform').before('<p class="response">'+data+'</p>');
                $('.response').slideDown();
                if(data=='Thank you, your message has been sent! <br /> We will review your submission and contact you as soon as possible.') $('#contactform').slideUp();
            }
        ); 
        return false;
    });
});
// ]]>
</script>
</head>
<body>
<form action="handler.php" method="post">

Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
Phone: <input type="text" name="phone"><br>
Address: <input type="text" name="address"><br>
City: <input type="text" name="city"><br>
ZIP: <input type="text" name="zip"><br>
Meal: <input type="text" name="meal"><br>
Spicy: <input type="text" name="spicy"><br>
Delivery: <input type="text" name="delivery"><br>
Message:<br><textarea rows="4" name="message" cols="28"></textarea><br><br>

<select data-placeholder="Select a category" name="category" id="selK6U" class="chzn-done">
    <option></option>
    <option value="Snack &amp; Quick Meals" data-connection="snackSelect">Snack &amp; Quick Meals</option>
    <option value="Wraps" data-connection="wrapSelect">Wraps</option>
    <option value="Breads" data-connection="breadSelect">Breads</option>
    <option value="Chicken Entrees" data-connection="chickenSelect">Chicken Entrees</option>
    <option value="Lahawajawaab Mutton" data-connection="laSelect">Lahawajawaab Mutton</option>
    <option value="Rice Dishes" data-connection="riceSelect">Rice Dishes</option>
    <option value="Vegetarian" data-connection="vegSelect">Vegetarian</option>
</select>

<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

PHP 处理程序

<?php
if(isset($_POST['email'])) {

$email_to = "email@myemail.com";
$email_subject = "subject";

function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted: <br />";
    echo $error."<br />";
    echo "Please fix these errors.<br />";
    die();
}

// validation expected data exists
if(!isset($_POST['name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['phone']) ||
    !isset($_POST['address']) ||
    !isset($_POST['city']) ||
    !isset($_POST['zip']) ||
    !isset($_POST['category']) ||
    !isset($_POST['meal']) ||
    !isset($_POST['spicy']) ||
    !isset($_POST['delivery']) ||
    !isset($_POST['message'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$address = $_POST['address']; // not required
$city = $_POST['city']; // not required
$zip = $_POST['zip']; // not required
$category = $_POST['category']; // not required
$meal = $_POST['meal']; // not required
$spicy = $_POST['spicy']; // not required
$delivery = $_POST['delivery']; // not required
$message = $_POST['message']; // not required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
        $string_exp = "/^[A-Za-z .'-]+$/";
          if(!preg_match($string_exp,$name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }


  if(strlen($error_message) > 0) {
    died($error_message);
  }
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "Zip: ".clean_string($zip)."\n";
$email_message .= "Meal Category: ".clean_string($category)."\n";
$email_message .= "Meal: ".clean_string($meal)."\n";
$email_message .= "How Spicy: ".clean_string($spicy)."\n";
$email_message .= "Delivery: ".clean_string($delivery)."\n";
$email_message .= "Other Details: ".clean_string($message)."\n";


// create email headers

$headers = "From: $email_from" . "\r\n" .
"Reply-To: $email_from" . "\r\n" .
"X-Mailer: PHP/" . phpversion();


mail($email_to, $email_subject, $email_message, $headers); 

    echo "Thank you, your message has been sent! <br /> We will review your submission and contact you as soon as possible.";

}
?>
于 2013-09-01T13:43:13.573 回答