Php 使用jquery显示成功的表单提交

Php 使用jquery显示成功的表单提交,php,jquery,Php,Jquery,我有以下名为优惠券.js的javascript文件- jQuery(document).ready(function(){ jQuery('.appnitro').submit( function() { $.ajax({ url : $(this).attr('action'), type : $(this).attr('method'), dataType: 'json',

我有以下名为优惠券.js的javascript文件-

jQuery(document).ready(function(){
        jQuery('.appnitro').submit( function() {
$.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                        for(var id in data) {
                            jQuery('#' + id).html( data[id] );
                        }
                      }

        });

        return true;
    });

});
sms.php-

<?php
//process form
$res = "message deliverd";
$arr = array( 'content' => $res );
echo json_encode( $arr );//end sms processing
unset ($_POST);
?>
请告诉我哪里出错了。我的javascript是正确的。firebug未显示任何错误或请告诉其他方法以实现所需功能。

甚至这个优惠券.js也不起作用-

jQuery(document).ready(function(e){
        jQuery('.appnitro').submit( function() {
$.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                        for(var id in data) {
                            jQuery('#' + id).html( data[id] );
                        }
                      }

        });

e.preventDefault();
    });

});

即使我在末尾添加了返回方式,也无法工作请建议其他方法来实现此功能

页面刷新的原因是未抑制
提交
事件

有两种方法可以做到这一点:

  • 接受事件对象作为事件处理程序的参数,然后对其调用
    preventDefault()
  • 从事件处理程序返回false

回答您的修订问题:您在错误的函数中接受了
e
参数,您应该在
submit
处理程序中接受它,而不是
ready
处理程序中接受它。

页面刷新的原因是
submit
事件未被抑制

有两种方法可以做到这一点:

  • 接受事件对象作为事件处理程序的参数,然后对其调用
    preventDefault()
  • 从事件处理程序返回false

回答您的修订问题:您在错误的函数中接受了
e
参数,您应该在
submit
处理程序中接受它,而不是
ready
处理程序中接受它。

我认为您需要在jquery中取消表单提交。从jQuery文档中:

现在,当表单提交时 消息已发出警报。这种情况发生在 到实际提交,所以我们可以 通过调用取消提交操作 事件对象上的.preventDefault() 或者通过从我们的 处理程序。我们可以触发事件 当另一个元素被激活时手动 点击:

因此,在您的代码中:

//add 'e' or some other handler to the function call   
 jQuery(document).ready(function(e){
            jQuery('.appnitro').submit( function() { $.ajax({
                url     : $(this).attr('action'),
                type    : $(this).attr('method'),
                dataType: 'json',
                data    : $(this).serialize(),
                success : function( data ) {
                            for(var id in data) {
                                jQuery('#' + id).html( data[id] );
                            }
                          }

            });
            //return false
            return false;

           //or
           e.preventDefault();
        });

    });

我认为您需要在jquery中取消表单提交。从jQuery文档中:

现在,当表单提交时 消息已发出警报。这种情况发生在 到实际提交,所以我们可以 通过调用取消提交操作 事件对象上的.preventDefault() 或者通过从我们的 处理程序。我们可以触发事件 当另一个元素被激活时手动 点击:

因此,在您的代码中:

//add 'e' or some other handler to the function call   
 jQuery(document).ready(function(e){
            jQuery('.appnitro').submit( function() { $.ajax({
                url     : $(this).attr('action'),
                type    : $(this).attr('method'),
                dataType: 'json',
                data    : $(this).serialize(),
                success : function( data ) {
                            for(var id in data) {
                                jQuery('#' + id).html( data[id] );
                            }
                          }

            });
            //return false
            return false;

           //or
           e.preventDefault();
        });

    });

+1或
preventDefault()
,但无论哪种方式,您都需要阻止默认表单提交。签出-.+1或
preventDefault()
,但无论哪种方式,你需要阻止默认表单提交。签出-。你的优惠券.js文件和jQuery api文件是否正确地包含在你的html表单文件中?是的…点击这里的抓取按钮链接-你上面的代码在我这边工作得很好,没有任何更改。但是我在html格式文件中包含了coupon.js文件和jquery-1.4.2.js,如下所示:
`
@NAVEED-我也做了同样的操作,但仍然没有成功。现在检查-尝试在success Function中执行alert json响应,看看会发生什么。您的优惠券.js文件和jQuery api文件是否正确地包含在html表单文件中?是的…单击此处的抓取按钮链接-您的上述代码在我这方面运行良好,没有任何更改。但是我在html格式文件中包含了coupon.js文件和jquery-1.4.2.js,如下所示:
`
@NAVEED-我也做了同样的操作,但仍然没有成功。现在检查-尝试在success Function中执行警报json响应,看看会发生什么。
//add 'e' or some other handler to the function call   
 jQuery(document).ready(function(e){
            jQuery('.appnitro').submit( function() { $.ajax({
                url     : $(this).attr('action'),
                type    : $(this).attr('method'),
                dataType: 'json',
                data    : $(this).serialize(),
                success : function( data ) {
                            for(var id in data) {
                                jQuery('#' + id).html( data[id] );
                            }
                          }

            });
            //return false
            return false;

           //or
           e.preventDefault();
        });

    });