我之前使用的是相同的代码,现在它工作正常,但它的工作原理是问题正在返回值。我测试从保存页面返回的值为“真实”,但是如果其他条件不起作用。jquery在if if else中使用成功值
<script>
function save(){
$.ajax({
type:'POST',
url:'save.php',
data:$('#form').serialize(),
success: function (result){
if (result=="true")
{
$("#message").html("saved!");
}
else
{
$("#message").html("oops!");
}
}
});
</script>
编辑:PHP保存表单到MySQL并返回true,我可以用警报(结果)得到它,但如果其他条件不关心它。 Jquery ver。 jQuery的2.1.1.min.js
<?php
$save=mysql_query("Insert into table_pr(textval) Values ('$textval')");
if($save>0)
{
json_encode("true");
}
else
{
json_encode("false");
}
?>
解决方案:dataType: 'json'
,这条线是失踪,页面保存我使用json_encode( “真”);现在它的作品感谢您的帮助。
只要使用'console.log(result)'和post,你会得到什么?还有一个输入错误的'}'末尾 – Satpal 2014-09-11 06:21:32
那么PHP在哪里呢? – Ghost 2014-09-11 06:22:41
难道是它实际返回的字符串值'“true”'? – morne 2014-09-11 06:25:07