将PHP中的字符串变量发送回带有数组变量的ajax ......?

时间:2012-04-26 13:09:14

标签: 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'"); //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填充消息的行。有什么想法吗?

6 个答案:

答案 0 :(得分:1)

您可以将该邮件添加为JSON属性之一,然后进行相应的搜索。

答案 1 :(得分:1)

你可以随时回复一下json编码的$ row。 添加$ row并将消息发送给数组变量,json编码并回显出来。

不是100%确定语法细节/点

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

答案 2 :(得分:0)

只需从服务器传递相应的消息。我们假设您的消息位于消息变量中:

$(“#res-message”)。html(''+ data.message +'预订ID位于,信息显示在'下方);

答案 3 :(得分:0)

当您将行数据转换为$ row时,它就是一个数组。如果你敢,你可以在json_encode之前将消息添加到该数组。

$row["message"] = ...

答案 4 :(得分: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);

答案 5 :(得分:0)

在ajax中发送。喜欢 不要回复你的if的正文中的任何内容,只需将消息存储在变量中,比如$message = 'passed',现在在php请求页面的末尾执行此操作:

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

这会将json数组作为响应发送,因此您可以在其中发送尽可能多的变量。只需将它们放入数组()并使用json_encode()将它们转换为json 转换为json并作为响应传递。收到ajax的成功函数后,只需解码两个json变量:message_jsrow_js

您可以使用jquery的parsejson来获取变量