[英]JQuery Ajax form serialize does not gives new result when submit first time
I have a trouble while submitting data with jquery ajax call 使用jquery ajax调用提交数据时遇到麻烦
the following jquery ajax call function I am using 我正在使用以下jquery ajax调用函数
$(".update-pages").bind('submit',function(){
var urlReq = "perform/update_pages.php?sbmt_change=&" +$(this).serialize();
$.ajax({
method : 'GET',
url : urlReq,
success : function(data){
alert(data);
}
});
return false;
});
The HTML form is: HTML形式为:
<form action="perform/update_pages.php" class="update-pages" method="GET">
<input type="hidden" value="about" id="getLoc" name="about" />
<textarea name="txt" rows="20">
</textarea>
<br />
<input type="submit" class="btn btn-primary btn-lg" value="UPDATE" name="sbmt_change" />
</form>
PHP file is ( perform/update_pages.php ): PHP文件为( perform / update_pages.php ):
if (isset($_GET['sbmt_change'])) {
$contents = urlencode($_GET['txt']);
$data = file_get_contents("http://localhost/web/about.php?contents=$contents");
$put = file_put_contents("../../../mysite/about.php", $data);
if ($put) {
echo 'updated';
exit();
}else{
echo 'unable to update pages';
exit();
}
}else{ // if not form submitted
header("location:../");
exit();
}
Problem : When I submit data, serialize()
send old result that was before change in textarea
, but when I submit again serialize()
sends new result, and the same trouble starts again and again. 问题 :当我提交数据时, serialize()
发送textarea
更改之前的旧结果,但是当我再次提交serialize()
发送新结果时,同样的麻烦又一次又一次地出现。 the even time submit send the new data but odd time submit sends old data. 偶数时间提交发送新数据,而奇数时间提交发送旧数据。
How I can solve this problem? 我该如何解决这个问题?
This is your code I just tested in my system and it is working fine. 这是我刚刚在系统中测试过的代码,并且工作正常。 I am getting proper data at each submission. 每次提交时我都会得到适当的数据。
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".update-pages").bind('submit',function(){
var urlReq = "server.php?sbmt_change=&" +$(this).serialize();
$.ajax({
method : 'GET',
url : urlReq,
success : function(data){
alert(data);
}
});
return false;
});
})
</script>
</head>
<body>
<form action="server.php" class="update-pages" method="GET">
<input type="hidden" value="about" id="getLoc" name="about" />
<textarea name="txt" rows="20">
</textarea>
<br />
<input type="submit" class="btn btn-primary btn-lg" value="UPDATE" name="sbmt_change" />
</form>
</body>
</html>
server side code : 服务器端代码:
<?php
if (isset($_GET['sbmt_change'])) {
print_r($_GET);
}else{ // if not form submitted
}
?>
Hi below is some changes in you code please use the modified code below 您好,下面是您代码中的一些更改,请使用下面的修改后的代码
<form action="" class="update-pages" method="GET">
<input type="hidden" value="about" id="getLoc" name="about" />
<textarea name="txt" rows="20">
</textarea>
<br />
<input type="submit" class="btn btn-primary btn-lg" value="UPDATE" name="sbmt_change" />
<script>
$(".update-pages").bind('submit', function () {
$.ajax({
method: 'POST', //also use GET
url: 'http://localhost:82/stack/get.php', //HERE IS FULL SITE URL OF YOUR FILE
data: $('.update-pages').serialize(),
success: function (data) {
alert(data);
}
});
return false;
});
</script>
get.php get.php
<?php
echo "<pre>";
print_r($_REQUEST);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.