jquery ajax结果消息

时间:2011-07-01 19:13:17

标签: php jquery mysql ajax custom-errors

我有一个php脚本的ajax调用,当用户提交数据字符串作为条目时,该脚本用新记录更新我的MySQL数据库。 PHP的一部分是if / else,它将检查条目是否重复。我可以阻止复制更新数据库,但我不知道如何发回“错误”消息,说“该记录已存在”...这是我的jquery:     [脚本]     $(function(){

    $(".entry_button").click(function() 
{
    var element = $(this);
    var boxval = $("#entry").val();
    var dataString = 'entry='+ boxval;
        if(boxval=='') {
            alert("Please Enter Some Text");
        } else {

            $("#flash").show();
            $("#flash").fadeIn(400).html('<img src="images/loading.gif" align="absmiddle">&nbsp;<span class="loading">Broifying It...</span>');
            $.ajax({
                    type: "POST",
                    url: "update_data.php",
                    data: dataString,
                    cache: false,
                    success: function(html){
                        $("ol#update").prepend(html);
                        $("ol#update li").slideDown("slow");
                        document.getElementById('entry').value='';
                        $("#flash").hide();
                    }

            });
        }
        return false;
    });
});
[/script]

这是我的PHP脚本更新DB(update_data.php):

[php]
include('db.php');
if(isset($_POST['entry']))
{
$date_created = date('Y-m-d H:i:s');
$entry = $_POST['entry'];
$query = "SELECT COUNT(*) AS count FROM table WHERE entry = '$entry'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if ($row['count'] == 0) {
mysql_query("insert into table (entry,date_created) values     ('$entry','$date_created')");
$sql_in= mysql_query("SELECT entry,id FROM table order by id desc");
$r=mysql_fetch_array($sql_in);
$newentry=$r['newentry'];
$id=$r['id'];
?>
<li class="entry bar<?php echo $id; ?>">
<div class="thebro"><span><?php echo $newentry; ?></span></div>
<div class="hr"></div>
<div class="utility-left"><span class="share"><a href="#" title="Share">share</a></span> | <span class="time">days ago</span></div>
<div class="utility-right"><span class="comments">COMMENTS</span> | <span  class="like">LIKE</span></div>
</li>
<?php
} else {
$error = "Sorry, that record already exists";
}   

}
[/php]

我希望能够从php脚本中吐出$error变量,以便在ajax调用中发回,并显示在我的HTML中的元素中。我用Google搜索了ajax错误消息,但它们都与标题错误有关,而不是验证错误消息。我也没有使用json_encode,除非有人想重做上面的那个jquery块,以便它作为json数据发送。

任何想法都会很棒!

3 个答案:

答案 0 :(得分:1)

如果在PHP中产生错误,我希望它由jquery ajax对象的错误方法处理,而不是通过发送正常的HTTP 200响应来伪造它,并在responseText上标记一些错误。

当从PHP返回HTTP 500时,ajax错误处理程序不会捕获“responseText”。错误处理程序接收XHR对象,状态为“错误”,并显示“内部服务器错误”错误消息。

我试图操纵错误信息..

header( 'HTTP/1.1 500 My Custom Error Message' );

但那没用。

这是什么工作:

function errorHandler( $number, $message, $file, $line ) {
    $data = array(
        'message' => $message,
        'line' => $line,
        'file' => $file,
        'trace' => debug_backtrace()
    );

    header( 'Debug: ' . json_encode( $data, true ) );
}

这将使PHP返回HTTP 500,并将您的自定义详细信息添加到额外的标题中。

从ajax错误处理程序访问自定义详细信息:

$.ajax({
    success: function( response ) {
        // ...
    },
    error: function( XHR, status, errorThrown ) {
        $.each( XHR.getAllResponseHeaders().split( '\n' ), function( i, header ) {
            if ( header.substr( 0, 7 ) == 'Debug: ' ) {
                var error = JSON.parse( header.substr( 7 ) );
                console.log( error );
            }
        });
    }
});

答案 1 :(得分:0)

只需在你的phpfile中回复它并使用:

document.getElementById('ajaxCatchbox').innerHTML = ajaxRequest.responseText;

来自你的ajaxrequest对象的responsetext拾取了所有回应的东西......不知道jquery中的等价物

答案 2 :(得分:0)

在PHP中回显错误并退出。然后,在你成功的开始:功能,做这样的事情:

    if(ajaxRequest.responseText.substring(0, 5) === 'Sorry') {
        alert(ajaxRequest.responseText);
        return;
    }

我建议使用子字符串方法,因为它允许您创建各种不同的错误消息,只要它们都以“抱歉”开头。用于调试。

相关问题