繁体   English   中英

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

[英]Sending string variable from PHP back to ajax with array variable…?

我想在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填充消息的行。 有任何想法吗?

您可以将该消息添加为JSON属性之一,然后适当地搜索它。

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

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

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

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

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

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

$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中发送两者。 LIke不要在你的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来获取变量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM