使用数组变量将字符串变量从PHP发送回ajax。。。?
我想在PHP变量中保存一条消息,并将其与已经返回的另一个数组变量一起发送回去。例如,我在PHP代码中进行了一些错误检查,希望返回一个带有特定消息的字符串变量,以便在javascript中使用 以下是PHP:使用数组变量将字符串变量从PHP发送回ajax。。。?,php,javascript,jquery,html,sql,Php,Javascript,Jquery,Html,Sql,我想在PHP变量中保存一条消息,并将其与已经返回的另一个数组变量一起发送回去。例如,我在PHP代码中进行了一些错误检查,希望返回一个带有特定消息的字符串变量,以便在javascript中使用 以下是PHP: <?php include('config-searchres.php'); $term = $_POST['resid']; $sql = mysql_query("SELECT * FROM ap_form_8 WHERE id = '$term'");
<?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>
$(文档).ready(函数(){
重置表格(“保留”);
$('#表单保留')。提交(函数(事件){
event.preventDefault();//表单提交时页面将不再刷新。
var resCheck=$(this).find('input[class=“reservationid”]”)。val();//现在我们有了预订ID,让我们执行检查。
$.ajax({
url:'inc/searchres.php',
键入:“POST”,
数据:“剩余=”+重新检查,
success:function(data){//data是从php文件返回的所有信息
$(“#保留id”).val(重新检查);//将读取id添加回文本框
var jsonData=$.parseJSON(data);//解析返回的JSON数据,这样我们就可以像data.name、data.where一样使用它
//****我希望下面的一行显示从PHP返回的适当消息****
$(“#res message”).html('找到预订ID,信息显示在下面');
$('json reservation')。填充({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'],预订状态:jsonData['ADD THIS CELL'],预订id:jsonData['id'],预订日期:jsonData'element_3'],预订时间:jsonData['4'],预订方:jsonData['element_5'],预订特殊要求:jsonData['element_6'],使用优惠券预订:jsonData['element_9'],预订优惠券代码:jsonData['element_10'],预订状态:jsonData['element_11'];
$(“#res cancel message”).html(“”);
},
错误:函数(){
$(“#res message”).html('您的请求有错误');
$(“#res cancel message”).html(“”);
}
});
});
});
我用星号标记了我现在用静态消息填充DIV的位置,这是我从PHP填充消息的行。有什么想法吗?您可以将该消息添加为JSON属性之一,然后进行适当的搜索。只需从服务器传递适当的消息即可。假设您的消息位于message变量中:
$(“#res message”).html(“”+data.message+“已找到预订ID,信息显示在下面”) 通过回显json编码的$row,您可以随时稍等片刻。 将$row和您的消息添加到数组变量中,并对其进行json编码和回显 语法细节/点不是100%确定
$response_array = array('message' => 'yourmessage', 'row' => $row);
echo json_encode($response_array);
当您将行数据转换为$row时,它就是一个数组。如果你敢的话,你可以在json_编码之前将你的消息添加到这个数组中
$row["message"] = ...
您可以这样做:
$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);
用ajax发送这两个文件。喜欢
不要在if-else中回显任何内容,只需将消息存储在变量中,例如,$message='passed'
,现在在php请求页面的末尾执行此操作:
echo json_encode(array('message_js'=>$message, 'row_js' => $row));
这会将json数组作为响应发送,这样您就可以在其中发送任意多的变量。只需将它们放在一个数组()中,并使用json\u encode()
转换为json并作为响应传递。在ajax的success函数中收到时,只需解码两个json变量:message_js
和row_js
您可以使用jquery的parsejson来获取变量,然后如何在JS中拆分这些变量?目前,我将数据数组设置为:var jsonData=$.parseJSON(数据);既然消息和数据现在都存储在这个数组中,我该如何分割它们?我该如何在JS中分割它们?目前,我将数据数组设置为:var jsonData=$.parseJSON(数据);既然消息和数据现在都存储在这个数组中,我该如何将它们分开呢?