2012-04-26 43 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級不同的消息,「合格」,「取消」和「未找到」 ......如果這些條件之一存在,我想把這個字符串發回給我的JavaScript,所以我可以在DIV中顯示它。不過,我也想發送$ row數據。

我的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

在阿賈克斯同時發送。像 不要在身體呼應東西你如果別的,該消息只是存儲在變數,比方說,$message = 'passed',現在做到這一點,在你的PHP請求頁面的末尾:

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

這將JSON數組作爲性反應等等你可以儘可能多地發送變量。只需將它們放在一個數組()中,並將它們轉換爲json,然後使用json_encode() 將其轉換爲json並作爲響應傳遞。當接收到ajax的成功函數時,只需解碼兩個json變量:message_jsrow_js

你可以使用jquery的parsejson來獲得你的變量然後

+0

我如何在JS中拆分它們?目前我設置數據數組爲:var jsonData = $ .parseJSON(data);由於消息和數據現在都存儲在這個數組中,我將如何將它們分開? – tmparisi 2012-04-26 17:32:05

+0

nm,我可以通過jsonData.status或.data訪問.....謝謝! – tmparisi 2012-04-26 21:38:32

1

您可以將該消息添加爲您的JSON屬性之一,然後進行適當的搜索。

0

只是通過從服務器相應的消息。讓我們假設你的消息是在消息變量:

$( 「#RES消息」)HTML( '' + data.message +'預訂ID位於,信息顯示在');

1

您可以隨時等待回聲json編碼的$行。 添加$行並將消息添加到數組變量,您可以將其編碼並回顯出來。

不是100%肯定有關語法細節/點

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

通過您轉換您的行數據到$行的時候,它是一個數組。如果你敢的話,你可以簡單地將你的消息添加到該數組中,然後對其進行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 2012-04-26 17:34:46

+0

nm,我可以通過jsonData.status或.data訪問.....謝謝! – tmparisi 2012-04-26 21:38:26