使用PHP进行jQuery AJAX表单数据序列化

时间:2022-11-24 13:26:22

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

我被困在我的代码中,我需要将表单中的数据发送到check.php页面然后进行处理。

This is my code:

这是我的代码:

The AJAX part:

AJAX部分:

<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:

php部分:

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

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

7 个解决方案

#1


9  

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);  
            }
        });
    });
    });

#2


7  

try it , but first be sure what is you response console.log(response) on ajax success from server

尝试一下,但首先要确定你对服务器的ajax成功响应console.log(响应)是什么

<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){
        if(response === 1){
            //load chech.php file  
        }  else {
            //show error
        }
        }
    });
});
});

#3


4  

<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>

This is perfect code , there is no problem.. You have to check that in php script.

这是完美的代码,没有问题..你必须在PHP脚本中检查。

#4


1  

I just had the same problem: You have to unserialize the data on the php side.

我只是遇到了同样的问题:你必须在php端反序列化数据。

Add to the beginning of your php file (Attention this short version would replace all other post variables):

添加到您的php文件的开头(注意这个简短版本将替换所有其他post变量):

parse_str($_POST["data"], $_POST);

#5


0  

Change your code as follows -

更改您的代码如下 -

<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){
        if(response == 1){
              $("#err").html("Hi Tony");//updated
        }  else {
            $("#err").html("I dont know you.");//updated
        }
        }
    });
});
});
</script>

PHP -

PHP -

<?php
$user=$_POST['user'];
$pass=$_POST['pass'];

if($user=="tony")
{
    echo 1;   
}
else
{
    echo 0;    
}
?>

#6


0  

Your problem is in your php file. When you use jquery serialize() method you are sending a string, so you can not treat it like an array. Make a var_dump($_post) and you will see what I am talking about.

你的问题出在你的php文件中。当您使用jquery serialize()方法时,您正在发送一个字符串,因此您不能将其视为数组。创建一个var_dump($ _ post),你会看到我在说什么。

#7


0  

Try this its working..

    <script>
      $(function () {
      $('form').on('submit', function (e) {

        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '<?php echo base_url();?>student_ajax/insert',
            data: $('form').serialize(),
            success: function (response) {
            alert('form was submitted');
            }
            error:function()
            {
            alert('fail');
            }
      });
      });
          });
    </script>

#1


9  

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);  
            }
        });
    });
    });

#2


7  

try it , but first be sure what is you response console.log(response) on ajax success from server

尝试一下,但首先要确定你对服务器的ajax成功响应console.log(响应)是什么

<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){
        if(response === 1){
            //load chech.php file  
        }  else {
            //show error
        }
        }
    });
});
});

#3


4  

<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>

This is perfect code , there is no problem.. You have to check that in php script.

这是完美的代码,没有问题..你必须在PHP脚本中检查。

#4


1  

I just had the same problem: You have to unserialize the data on the php side.

我只是遇到了同样的问题:你必须在php端反序列化数据。

Add to the beginning of your php file (Attention this short version would replace all other post variables):

添加到您的php文件的开头(注意这个简短版本将替换所有其他post变量):

parse_str($_POST["data"], $_POST);

#5


0  

Change your code as follows -

更改您的代码如下 -

<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){
        if(response == 1){
              $("#err").html("Hi Tony");//updated
        }  else {
            $("#err").html("I dont know you.");//updated
        }
        }
    });
});
});
</script>

PHP -

PHP -

<?php
$user=$_POST['user'];
$pass=$_POST['pass'];

if($user=="tony")
{
    echo 1;   
}
else
{
    echo 0;    
}
?>

#6


0  

Your problem is in your php file. When you use jquery serialize() method you are sending a string, so you can not treat it like an array. Make a var_dump($_post) and you will see what I am talking about.

你的问题出在你的php文件中。当您使用jquery serialize()方法时,您正在发送一个字符串,因此您不能将其视为数组。创建一个var_dump($ _ post),你会看到我在说什么。

#7


0  

Try this its working..

    <script>
      $(function () {
      $('form').on('submit', function (e) {

        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '<?php echo base_url();?>student_ajax/insert',
            data: $('form').serialize(),
            success: function (response) {
            alert('form was submitted');
            }
            error:function()
            {
            alert('fail');
            }
      });
      });
          });
    </script>