-1

我在使用 PHP 联系表单时遇到了一些问题,提交时此表单仅返回电话号码:并且还查询:未定义。它应该返回姓名、电话号码和我的查询。知道为什么它不返回名称以及为什么 Inquiry 未定义吗?

我的 PHP

    $name=$_POST['name'];
    $phone=$_POST['phone'];
    $enquiry=$_POST['enquiry'];

    $subject="NINJA2k - EMAIL INQUIRY FROM: $name";
    $message="
          Phone:<br>
          <strong>$phone</strong><br>
          <br>
          Enquiry:<br>
          <strong>$enquiry</strong><br>
          <br>
    ";

    mysql_close();

    include("class.phpmailer.php");
    include("class.smtp.php");

    $mail = new PHPMailer();
    $mail->IsSMTP();// send via SMTP
    $mail->Host = "localhost"; // SMTP servers
    $mail->SMTPAuth = false; // turn on/off SMTP authentication
    $mail->From = "user1@mydomain.com";
    $mail->FromName = "http://mail.mydomain.com/";
    $mail->AddAddress("user1@mydomain.com");
    $mail->AddReplyTo("user1@mydomain.com");
    $mail->WordWrap = 50;// set word wrap
    //now Attach all files submitted

    $mail->Body = $message;
    //
    $mail->IsHTML(true);// send as HTML
    $mail->Subject = $subject;


    if($mail->Send()) 
    {
        echo "Thank You <b>$name</b><br>Your email has been sent.";
    } 
    else 
    {
        echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
    }
?>

$('.contact_form').dialog({
        width:500,
        height:450,
        modal:true,
        buttons: {
            "Send": function() {
                if(($("#ct_name").val()=="") || ($("#ct_phone").val()=="") || ($("#ct_enquiry").val()=="") || ($("#ct_captcha").val()==""))
                    alert("You must fill all the field");
                else
                {
                    var capt=$(div_list[ind]).attr("id");
                    if(capt!=$("#ct_captcha").val().toLowerCase())
                        alert("Captcha Code is Invalid!");
                    else
                    {
                        var send_string;
                        send_string="name=";
                        send_string+=$("#ct_name").val();
                        send_string+="&phone=";
                        send_string+=$("#ct_phone").val();
                        send_string+="&enquiry=";
                        send_string+=$("#ct_enquiry").val();

                        $.ajax({
                             type: "POST",
                             url: "send_mail.php",
                             data:   send_string,
                             async: false,
                             success: function(msg){ 
                                alert("Your mail was sent successfully!");
                             } ,
                             error: function(msg){
                                alert("Sorry! We couldn't send email"); 
                             }
                        }); 
                    }
                }

            }
        }
    });

我的 HTML

    <div class="contact_form" title="Dialog Title" style="display:none">
    <table width="100%" style="padding:30px">
        <tr><td>Name</td><td><input type="text" id="ct_name"/></td></tr>
        <tr><td>Phone Number</td><td><input type="text" id="ct_phone"/></td></tr>
        <tr><td>Enquiry Data</td><td><textarea rows="4"></textarea></td></tr>
        <tr>
        <td>Captcha</td>
        <td>
            <div>
                <div class="captcha" id="28ivw"><img src="images/captcha1.png" /></div>
                <div class="captcha" id="k4ez"><img src="images/captcha2.png" /></div>
                <div class="captcha" id="jw62k"><img src="images/captcha3.png" /></div>
                <div class="captcha" id="fh2de"><img src="images/captcha4.png" /></div>
                <div class="captcha" id="gwprp"><img src="images/captcha5.png" /></div>
                <div class="captcha" id="4d7ys"><img src="images/captcha6.png" /></div>
                <div class="captcha" id="e5hb"><img src="images/captcha7.png" /></div>
                <div class="captcha" id="xmqki"><img src="images/captcha8.png" /></div>
                <div class="captcha" id="6ne3"><img src="images/captcha9.png" /></div>
                <div class="captcha" id="xdhyn"><img src="images/captcha10.png" /></div>
                <div class="captcha" id="q98p"><img src="images/captcha11.png" /></div>
                <div class="captcha" id="hrai"><img src="images/captcha12.png" /></div>
            </div>
            <input type="text" style="margin-top:15px" id="ct_captcha"/>
        </td>
        </tr>
    </table>
</div>
4

1 回答 1

2

您需要name在 HTML 中使用属性才能达到该值。所以而不是

<tr><td>Name</td><td><input type="text" id="ct_name"/></td></tr>

你应该有

<tr><td>Name</td><td><input type="text" name="name" id="ct_name"/></td></tr>

编辑

“技巧”是将name值用作$_POST数组中输入的键。但是如果你同时使用 jQuery,那么你可能仍然需要id或者class因为在 DOM 中使用它们比使用名称更容易到达输入。

于 2012-12-04T12:23:40.837 回答