jQuery AJAX 表单数据使用 PHP 序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24007780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery AJAX form data serialize using PHP
提问by gomzy
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.";
}
回答by Harish U Warrier
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);
}
});
});
});
回答by Always Sunny
try it , but first be sure what is you response console.log(response) on ajax success from server
尝试一下,但首先要确定您在服务器上的 ajax 成功时响应 console.log(response) 是什么
<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
}
}
});
});
});
回答by Ananta Prasad
<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 脚本中检查。
回答by ToHe
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 文件的开头(注意这个简短版本将替换所有其他帖子变量):
parse_str($_POST["data"], $_POST);
回答by karthikeyan
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>
回答by Alcides
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 文件中。当您使用 jqueryserialize()
方法时,您正在发送一个字符串,因此您不能将其视为数组。做一个var_dump($_post)
,你就会明白我在说什么。
回答by Chetan Gawai
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;
}
?>