Php jQuery发布表单数据

Php jQuery发布表单数据,php,jquery,ajax,Php,Jquery,Ajax,单击submit时,我希望所有表单数据都发布到process.php。然后在process.php上,我想回显POST数据,并最终将results div中的所有内容替换为process.php中的内容 <script type="text/javascript"> $(document).ready(function(){ $("#myform").submit( function () { $.ajax({

单击submit时,我希望所有表单数据都发布到process.php。然后在process.php上,我想回显POST数据,并最终将results div中的所有内容替换为process.php中的内容

<script type="text/javascript">
    $(document).ready(function(){
        $("#myform").submit( function () {    
            $.ajax({   
                type: "POST",
                dataType: "html", 
                cache: false,  
                url: "process.php",   
                success: function(data){
                    $("#results").html(data);                       
                }   
            });   
            return false;   
        });

        //$("#myform").submit( function () {
            //$('#results').html("yay");                    
        //}  
            // });  
        //} );          
    });
</script>

$(文档).ready(函数(){
$(“#myform”).submit(函数(){
$.ajax({
类型:“POST”,
数据类型:“html”,
cache:false,
url:“process.php”,
成功:功能(数据){
$(“#结果”).html(数据);
}   
});   
返回false;
});
//$(“#myform”).submit(函数(){
//$('#results').html(“yay”);
//}  
// });  
//} );          
});


快速移动


测试它

您可以调用
$.post
传递序列化的表单数据。像这样:

<script type="text/javascript">
        $(document).ready(function(){
            $("#myform").submit( function () {    
              $.post(
               'process.php',
                $(this).serialize(),
                function(data){
                  $("#results").html(data)
                }
              );
              return false;   
            });   
        });
</script>

$(文档).ready(函数(){
$(“#myform”).submit(函数(){
美元邮政(
“process.php”,
$(this).serialize(),
功能(数据){
$(“#结果”).html(数据)
}
);
返回false;
});   
});

只有另一种方法可以做到这一切。本例使用jQuery验证插件。在这里,所有表单字段都会自动验证。 从此处下载jquery:

从此处下载验证插件:


$(文档).ready(函数(){
$(“#自定义表单”)。验证({
调试:错误,
规则:{
名称:{required:true,minlength:8,maxlength:8},
电子邮件:{required:true,email:true},
},
submitHandler:函数(表单){
//为有效的表单做其他事情
$('#rsltx').html('正在处理…请稍候');
$.post('post.php',$(“#customForm”).serialize(),函数(数据){
$('#rsltx').html(数据);
});
}
});
});
名称
电子邮件

那么,问题是什么?您的代码似乎就是这么做的(冷检查)。有什么问题吗?keepitterron那段代码有效,但现在我想改变一件事。。。当用户从列表中选择某个内容时,我已经并希望能够运行上述代码
$("#myform").submit( function () {    
    $.ajax({   
        type: "POST",
        data : $(this).serialize(),
        cache: false,  
        url: "process.php",   
        success: function(data){
            $("#results").html(data);                       
        }   
    });   
    return false;   
});
<script type="text/javascript">
        $(document).ready(function(){
            $("#myform").submit( function () {    
              $.post(
               'process.php',
                $(this).serialize(),
                function(data){
                  $("#results").html(data)
                }
              );
              return false;   
            });   
        });
</script>
    <script type="text/javascript" src="jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="jquery.validate.1.7.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $("#customForm").validate({
    debug: false,
    rules: {
      name: {required:true,minlength:8,maxlength:8},
      email: {required:true,email:true},
    },
        submitHandler: function(form) {
     // do other stuff for a valid form 
     $('#rsltx').html('<img src="WhiteLoading.gif"> Processing... please wait');
     $.post('post.php', $("#customForm").serialize(), function(data) {
                        $('#rsltx').html(data);
         });
        }
  });
});
</script>

<form method="post" id="customForm" action="">
<div>
 <label for="name">Name</label>
 <input id="name" name="name" type="text" autocomplete="off" required/>
</div>
<div>
 <label for="email">E-mail</label>
 <input id="email" name="email" type="email" autocomplete="off" required/>
</div>
<div>
 <input id="send" name="send" type="submit" value="Send" />
</div>
</form>