使用PHP jQuery的AJAX表单数据序列化 [英] jQuery AJAX form data serialize using PHP

查看:146
本文介绍了使用PHP jQuery的AJAX表单数据序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我停留在我的code,我需要从表单发送数据到check.php页面,然后对其进行处理。

这是我的code:

AJAX的部分:

 <脚本SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js>< / SCRIPT&GT ;
<脚本>
$(文件)。就绪(函数(){
变种形式= $(#myForm会);
$(#SMT)。点击(函数(){
$阿贾克斯({
        键入:POST,
        网址:form.attr(行动),
        数据:form.serialize(),
        成功:函数(响应){
            的console.log(响应);
        }
    });
});
});
< / SCRIPT>
 

形式:

 <形式的行动=check.php方法=邮报NAME =myForm的ID =myForm的>
<输入类型=文本名称=用户ID =用户/>
<输入类型=文本名称=通行证ID =通/>
<输入类型=按钮NAME =SMTVALUE =提交ID =SMT/>
< /形式GT;
< D​​IV ID =犯错>< / DIV>
 

PHP的一部分:

  $用户= $ _ POST ['用户'];
$传球= $ _ POST ['传球'];

如果($用户==托尼)
{
    回声HI$用户。
}
其他
{
    回声我不认识你。
}
 

解决方案

试试这个

  $(文件)。就绪(函数(){
    变种形式= $(#myForm会);
    $(#SMT)。点击(函数(){
    $阿贾克斯({
            键入:POST,
            网址:form.attr(行动),
            数据:$(#myForm会输入)序列化(),//只输入
            成功:函数(响应){
                的console.log(响应);
            }
        });
    });
    });
 

I am stuck in my code, I need to send data from the form to the check.php page and then process it.

This is my code:

The AJAX part:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var form=$("#myForm");
$("#smt").click(function(){
$.ajax({
        type:"POST",
        url:form.attr("action"),
        data:form.serialize(),
        success: function(response){
            console.log(response);  
        }
    });
});
});
</script>

The form:

<form action="check.php" method="post" name="myForm" id="myForm">
<input type="text" name="user" id="user" />
<input type="text" name="pass" id="pass" />
<input type="button" name="smt" value="Submit" id="smt" />
</form>
<div id="err"></div>

the php part:

$user=$_POST['user'];
$pass=$_POST['pass'];

if($user=="tony")
{
    echo "HI ".$user;   
}
else
{
    echo "I dont know you.";    
}

解决方案

Try this

 $(document).ready(function(){
    var form=$("#myForm");
    $("#smt").click(function(){
    $.ajax({
            type:"POST",
            url:form.attr("action"),
            data:$("#myForm input").serialize(),//only input
            success: function(response){
                console.log(response);  
            }
        });
    });
    });

这篇关于使用PHP jQuery的AJAX表单数据序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆