2012-04-26 11 views
0

私はPHP変数にメッセージを保存して、もう戻ってきた他の配列変数に戻したいと思います。例えば、私はPHPコードの内部で行われるいくつかのエラーチェックを持っていて、特定のメッセージを持つ文字列変数をjavascriptで使用するために送り返したいと思います。ここで配列変数を使用してPHPからAJAXに文字列変数を戻す...?

はPHPです:

<?php 
    include('config-searchres.php'); 

    $term = $_POST['resid']; 
    $sql = mysql_query("SELECT * FROM ap_form_8 WHERE id = '$term'"); //select first name (element_1_1) from form #8 

    if ($row = mysql_fetch_array($sql)){ //if reservation number exists 
     if ($row['element_11'] != 'Cancelled'){ //if reservation has not already been cancelled 
      if (strtotime($row['element_3']) >= strtotime(date("Y-m-d"))){ //if reservation has not already passed date 
       echo json_encode($row); 
      } 
      else //Reservation already passed (old reservation) 
      { 
       echo 'passed'; 
      } 
     } 
     else //Reservation already cancelled 
     { 
      echo 'cancelled'; 
     } 
    } 
    else //Reservation not found 
    { 
     echo 'not found'; 
    } 
    mysql_close(); 
?> 

あなたが見ることができるように、3種類のメッセージがある、「合格」、「キャンセル」、および「が見つかりません」...これらの条件のうちの1つが存在する場合、この文字列をjavascriptに送ってDIVに表示することができます。しかし、私はまた$行のデータをそれと一緒に送信したいと思っています。

私のjavascript:

<script type="text/javascript"> 
     $(document).ready(function(){ 
      resetForms('reservation'); 
      $('#form-reservation').submit(function(event){ 
       event.preventDefault(); //the page will no longer refresh on form submit. 
       var resCheck = $(this).find('input[class="reservationid"]').val(); //now we have the reservation ID, let's perform our check. 
       $.ajax({ 
        url: 'inc/searchres.php', 
        type: 'POST', 
        data: 'resid='+resCheck, 
        success: function(data){ //data is all the info being returned from the php file 
         $('#reservation-id').val(resCheck); //add read ID back into text box 
         var jsonData = $.parseJSON(data); //parse returned JSON data so we can use it like data.name, data.whatever 
          //****I wanted the line just below this to display the appropriate message sent back from the PHP**** 
$("#res-message").html('<a>Reservation ID Located, Information is displayed below</a>'); 
          $('#json-reservation').populate({personal_first_name:jsonData['element_1_1'],personal_last_name:jsonData['element_1_2'],personal_phone_1:jsonData['element_7'],personal_email:jsonData['element_2'],reservation_status:jsonData['ADD THIS CELL'], reservation_id:jsonData['id'], reservation_date:jsonData['element_3'],reservation_time:jsonData['element_4'],reservation_party:jsonData['element_5'],reservation_special_request:jsonData['element_6'],reservation_using_coupon:jsonData['element_9'],reservation_coupon_code:jsonData['element_10'],reservation_status:jsonData['element_11']}); 
          $("#res-cancel-message").html(''); 
        }, 
        error: function(){ 
         $("#res-message").html('<a>There was an error with your request</a>'); 
         $("#res-cancel-message").html(''); 
        } 
       }); 
      }); 
     }); 
     </script> 

私はこの時点では、静的なメッセージとDIVを移入アスタリスクでマークされたが、これは私がPHPからメッセージを取り込みますラインです。何か案は?

答えて

0

をAJAXの両方を送信します。 いけないの身体には何もエコーのように、単に変数、たとえば、$message = 'passed'にメッセージを保存し、他の場合は、あなた、今お使いのPHPの要求ページの末尾に次の操作を行います。

echo json_encode(array('message_js'=>$message, 'row_js' => $row)); 

これはresponceとしてJSON配列を送信しますので、あなたが好きなだけたくさんの変数を送ることができます。配列()に入れて、json_encode() を使用してjsonに変換し、jsonに変換して応答として渡します。あなたのajaxの成功関数を受け取ったら、ちょうど2つのjson変数:message_jsrow_jsをデコードしてください。

jqueryのparsejsonを使用して変数を取得することができます

+0

これらをJSでどのように分割するのですか?現在、データ配列を次のように設定しています。var jsonData = $ .parseJSON(data);メッセージとデータはどちらもこの配列に格納されているので、どのように分割するのですか? – tmparisi

+0

nm、jsonData.statusまたは.dataでアクセスできます.....ありがとう! – tmparisi

1

JSONプロパティの1つとしてメッセージを追加し、それを適切に検索することができます。

0

ちょうど私達があなたのメッセージがメッセージ変数であるとし.letサーバから適切なメッセージを渡します。

$( "#のRESメッセージ")HTML( '' + data.message + '予約をIDが見つかりました、情報が下に表示されます ');

1

json encoded $ rowをエコーすることでいつでも少し待つことができます。 $ rowを追加し、jsonエンコードしてエコーアウトする配列変数にメッセージを送ります。

ない

$response_array = array('message' => 'yourmessage', 'row' => $row); 
echo json_encode($response_array); 
0

構文の詳細/ドット約100%必ず$行にあなたの行データを変換する時までに、それが配列です。もしあなたが勇気があれば、json_encodeする前にその配列にメッセージを追加することができます。

$row["message"] = ... 
0

あなたはそれをこのように行うことができます。

$result = array(); 

if ($row = mysql_fetch_array($sql)){ //if reservation number exists 
    if ($row['element_11'] != 'Cancelled'){ //if reservation has not already been cancelled 
     if (strtotime($row['element_3']) >= strtotime(date("Y-m-d"))){ //if reservation has not already passed date 
      $result = array('status' => 'OK', 'data' => $row); 
     } 
     else //Reservation already passed (old reservation) 
     { 
      $result = array('status' => 'passed', 'data' => $row); 
     } 
    } 
    else //Reservation already cancelled 
    { 
     $result = array('status' => 'cancelled', 'data' => $row); 
    } 
} 
else //Reservation not found 
{ 
    $result = array('status' => 'not found', 'data' => null); 
} 

echo json_encode($result); 
+0

これらをJSでどのように分割しますか?現在、データ配列を次のように設定しています。var jsonData = $ .parseJSON(data);メッセージとデータはどちらもこの配列に格納されているので、どのように分割するのですか? – tmparisi

+0

nm、jsonData.statusまたは.dataでアクセスできます.....ありがとう! – tmparisi

関連する問題