0

私は答えを探していましたが、それでも、それを行う方法がわかりません。正直、プログラミングは苦手でまだまだ勉強中です。

以下は私のコードです:

$.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 によって提供されたコードを使用しています。問題は、テキストフィールド/フォームアイテムを使用して作業する方法がわからないことです。ここに私の更新されたコードがあります:

    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();

                }

これには何か問題があることを知っています。お願いします?私がこれについて助けを求めるのはこれが最後です。ありがとう、あなたのすべての答えは非常に高く評価されます!

4

3 に答える 3

0

次に、おそらく通常の結果で行っているように$output、エラーを含む を返す必要があります。json_encoded()いくつかのエラー メッセージを含む配列をエンコードすることもできます。

于 2012-09-03T11:33:53.473 に答える
0
 $.ajax({
  type: "POST",
  url: "process.php",
  data: {
  postid: post_id
  },
  success: function(data){
  document.getElementById('processed').innerHTML = data;
  }

あなたの process.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でその出力を表示します

于 2012-09-03T11:37:45.833 に答える
0

It sounds like what you want is JSON. Then you can check if the error field is empty and show an error if it is not. Try something like the below.

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);
}

And the 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
        }
    }
});

Then you can simply return other data in $result['something_else'] and show that if there was no error. Also, this solution gives you a lot of flexibility to choose where you want to show your data. For instance, you may want to show your error somewhere else than the rest of the data you return from PHP. If you just echo the data out in PHP, you are forced to show it at the same place, and then you have to rely on CSS to change the positioning.

Hope it helps.

于 2012-09-03T11:40:59.280 に答える