在if-else中使用Jquery表单成功值

Jquery form success value using in if else

本文关键字:表单 成功 Jquery if-else      更新时间:2023-09-26

我以前使用过相同的代码,现在运行良好,但问题是返回值。我测试从保存页面返回的值为"true",但在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,我可以用alert(result)获得它,但如果其他条件不关心它。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("true");现在有效了,谢谢你的帮助。

字符串的开头和结尾都包含"符号,因此请检查字符串是否等于"true"。

FIRST

PHP的mysql_query成功时返回TRUE,失败时返回FALSE对0尝试true,对else语句尝试false

第二次

你为什么要使用解决方案

<?php
if(mysql_query("Insert into table_pr(textval) Values ('$textval')"))
{
            echo "true";
}
        else
        {
            echo "false";
        }
?>

应该是这样的。代码整洁!