从另一个PHP文件中获取POST方法的错误


Getting the error of POST method from another PHP file

我一直在寻找答案,但仍然不知道如何做到。老实说,我不太擅长编程,我还在学习中。

下面是我的代码:

$.ajax({
type: "POST",
url: "process.php",
data: {
postid: post_id
},
success: function(){
document.getElementById('processed').innerHTML = post_id;
...

我对此没有任何问题,只是我想在process.php上得到错误(如果有的话)。

process.php代码,我想在那里得到错误:

  if($result){
      while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
            $m = $row['processedID'];
            $id = trim($_POST ['postid']);
        try {
            ..something...
      }
       catch (APIHandle $e) {
            $output .= "<p>'". $row['name'] . "' processing failed</p>";
         }
}
}

我想接这条电话:

 $output .= "<p>'". $row['name'] . "' processing failed</p>";

并将其作为错误显示在我的第一个php文件中,并将其输出为文本。例如,用户单击一个按钮,它会检查输入,并在使用这些代码时输出错误。

谢谢你的帮助!

编辑我现在使用andy提供的代码,因为它可以处理错误,并为我提供了一个在发生错误时该怎么办的选项。问题是我不知道如何使用textfields/form项来处理它。这是我更新的代码:

    var textfieldvalue = $("#text").val();
    var post_id = textfieldvalue;
    $.ajax({
            type: "POST",
            url: "process.php",
            dataType: "json",
            data: {
                postid: post_id
            },
            success: function(result){
                 if (result.error) {
                    $('#accesserror').show();
                    $('#error').html(result.error).show();
                }
                else {
                    $("#loading").hide();
                    $("#processor").show();
                }

我知道这个有问题。请这是我最后一次寻求帮助。谢谢你,我们将非常感谢你的回答!

然后您应该将包含错误的$output返回为json_encoded(),就像您可能对正常结果所做的那样。您也可以用几个错误消息对数组进行编码。

 $.ajax({
  type: "POST",
  url: "process.php",
  data: {
  postid: post_id
  },
  success: function(data){
  document.getElementById('processed').innerHTML = data;
  }

您的流程.php

  if($result){
  while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        $m = $row['processedID'];
        $id = trim($_POST ['postid']);
    try {
        ..something...
    }
    catch (APIHandle $e) {
        echo $output .= "<p>'". $row['name'] . "' processing failed</p>";
     }
  }

如果您的代码在process.php中捕获,它将返回输出,并在ajax dispay中输出

听起来您想要的是JSON。然后,您可以检查error字段是否为空,如果不是,则显示错误。试试下面这样的东西。

if($result) {
    $result = array();
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $m = $row['processedID'];
        $id = trim($_POST ['postid']);
        try {
            // something
        }
        catch (APIHandle $e) {
            $result['error'] = 'Processing failed!';
        }
    }
    echo json_encode($result);
}

以及jQuery:

$.ajax({
    type: "POST",
    url: "process.php",
    dataType: "json",
    data: {
        postid: post_id
    },
    success: function(result){
        // There was an error
        if (result.error) {
            $('#error').html(result.error).show();
        }
        // No error
        else {
            // Something
        }
    }
});

然后,您可以简单地在$result['something_else']中返回其他数据,并在没有错误的情况下进行显示。此外,此解决方案为您提供了很大的灵活性,可以选择要显示数据的位置。例如,您可能希望在其他地方显示您的错误,而不是从PHP返回的其余数据。如果你只是在PHP中回显数据,你会被迫在同一个地方显示它,然后你必须依靠CSS来改变定位。

希望能有所帮助。