tmparisi tmparisi - 1 year ago 64
PHP Question

Sending string variable from PHP back to ajax with array variable...?

I would like to save a message in PHP variable and send it back with my other array variable that is already coming back. For instance, I have some error checking that takes place inside the PHP code and would like a string variable with the specific message sent back for use in my javascript.

Here is the 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';

As you can see, there are 3 different messages, "passed", "cancelled", and "not found"... if one of these conditions exists, I would like to send this string back to my javascript so I can display it in a DIV. However, I also want to send the $row data with it.

My javascript:

<script type="text/javascript">
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.
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.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']});
error: function(){
$("#res-message").html('<a>There was an error with your request</a>');

I marked with asterisks where I populate the DIV with a static message at this time, this is the line where I would populate the message from PHP. Any ideas?

Answer Source

Send both in ajax. LIke Dont echo anything in the body of your if else, just store the message in variable, say, $message = 'passed', now do this at the end of your php request page:

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

This sends json array as responce so u can send as much variables in it as much u like. Just put them in an array() and convert them into json using json_encode() to convert into json and pass as response. When recieved in success function of your ajax, just decode the two json variables : message_js and row_js.

You can use parsejson of jquery to get your variables then