【问题标题】:ajax is working but form data not getting submittedajax 正在工作,但没有提交表单数据
【发布时间】:2017-03-26 09:08:47
【问题描述】:

我使用了一个淡出并显示感谢信息的 ajax 联系表单。我在提交表单后也收到了邮件。但它没有在表单中输入任何数据。我认为表单数据没有传递给 handler.php..有什么帮助吗?

ajax.js

$(function() {

var theForm = $("#memberform");

theForm.validate({

submitHandler: function(theForm) {

$('#loader', theForm).html('Please Wait...');

$.ajax({
type: "POST",
url: "handler.php",
data: $(theForm).serialize(),
timeout: 20000,

    success: function(msg) { $(theForm).fadeOut((500, function(){ 

                $(theForm).html("<h2>Thank you. We will contact you shortly.</h2>").fadeIn(); 

                }));
            },

            error: $('.thanks').show()

        });

        return false;
    }
   });
});

handler.php

<?php


$to      = 'something@gmail.com';
$subject = 'Contact Form';

$name = $_POST['senderName'];
$phone = $_POST['senderNumber'];
$email = $_POST['senderEmail'];
$message = $_POST['senderComments'];

$MESSAGE_BODY = "Name: ".$name."<br>"; 
$MESSAGE_BODY .= "Contact No: ".$phone."<br>"; 
$MESSAGE_BODY .= "Email: ".$email."<br>"; 
$MESSAGE_BODY .= "Message: ".nl2br($message)."<br>"; 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'From: Companyname' . "\r\n";
$headers .= 'Reply-To: something.com' . "\r\n";
$headers .= 'Cc: something@gmail.com' . "\r\n";
//$headers .= 'Bcc: example@example.com' . "\r\n";


mail($to, $subject, $MESSAGE_BODY, $headers);


?

我在 body 标签顶部的 head 标签中链接的脚本。

<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript" src="met.js"></script>

我使用重写规则编辑了 web.config 文件以隐藏域中的 .php 扩展...

<rewrite>
        <rules>
    <rule name="Hide .php ext">
        <match ignoreCase="true" url="^(.*)"/>
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
            <add input="{REQUEST_FILENAME}.php" matchType="IsFile"/>
        </conditions>
        <action type="Rewrite" url="{R:0}.php"/>
    </rule>
    <rule name="Redirecting .php ext" stopProcessing="true">
        <match url="^(.*).php"/>
        <conditions logicalGrouping="MatchAny">
            <add input="{URL}" pattern="(.*).php"/>
        </conditions>
        <action type="Redirect" url="{R:1}"/>
    </rule>
   </rules>
</rewrite>

【问题讨论】:

  • 代替$(theForm).serialize() 试试theForm.serialize()。已经是 jQuery 对象了,不需要再通过 jQuery 传递。
  • 嗨,感谢您的帮助。编辑后,ajax 根本不起作用。它重定向到 handler.php 并保持不变。 @maiorano84
  • 你序列化的实际表单在哪里?

标签: php ajax contact-form ajaxform


【解决方案1】:

试试这个,也许有帮助

$(function() {
var theForm = $("#memberform");
var data =  theForm.serialize();
theForm.validate({
    submitHandler: function(theForm) {
        $('#loader', theForm).html('Please Wait...');
        $.ajax({
            type: "POST",
            url: "handler.php",
            data: data,
            timeout: 20000,
            success: function(msg) { $(theForm).fadeOut((500, function(){ 
                    $(theForm).html("<h2>Thank you. We will contact you shortly.</h2>").fadeIn();
                }));
            },
            error: $('.thanks').show();
        });return false;
    }
   });
});

【讨论】:

  • 不工作我的朋友... ajax 不工作。它只重定向到 handler.php。
  • 好的,在 handler.php 中写 echo "
    ";print_r($_POST);exit;
    和 chcek 是不是从 ajax 解析数据
  • 我在 php 中不太专业...所以我应该删除 handler.php 中的所有内容并编写上面的代码吗?那么ajax.js ..我应该对此进行编辑吗? @Soni Vimal
  • 不要在你的代码开始之前写下这一行
【解决方案2】:

感谢您帮助我解决问题。刚刚通过删除一些重写规则找到了答案。问题出在 web.config 中。删除重写规则使表单工作正常...

或者,如果您想保留重写规则并提交表单,请编辑 --- url: "handler.php"--- 到 url: "handler" 的行,表单工作得很好。

【讨论】:

    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 2017-12-26
    • 1970-01-01
    相关资源
    最近更新 更多