php - 在 JSON 的响应对象上调用 Ajax

标签 php jquery json cakephp

我正在使用来自 json 的 ajax 调用。我从 ajax 调用中调用酒店列表,但现在我必须调用特定酒店的价格。请多多支持。

酒店列表代码如下:
HTML

<td style="color: #993300"><strong>Add Services<input name="add-service" id="add-service" type="button" value="+" style="background-color: #993300; color: #FFFFFF;" /></strong></td>

JQuery调用ajax获取酒店列表:

$('#cmb_service').bind('change', function(){

        var value = $(this).val();
        var destination = $( "#destination" )
        $.ajax({
                type : 'POST',
                url : '../enquiries/getpricebyajax',
                dataType : 'json',
                data: {
                        service : value,
                        destno : destination.val()
                },
                success : function(data) {
                       $('#waiting').hide(500);
                       $('#divserviceprovider').text('');
                       $('#divserviceprovider').append(data.msg);
                       $('#divserviceprovider').show(500);
                       if (data.error === true)
                            $('#divserviceprovider').show(500);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                        $('#waiting').hide(500);
                        $('#divserviceprovider').removeClass().addClass('error')
                            .text('There was an error.').show(500);
                        $('#divserviceprovider').show(500);
                }
        });
        return false;
});`

酒店列表响应PHP代码如下:

function getpricebyajax()

{
            $str="";$substr="";
            if(!empty($_POST['service']))
                {
                    switch ($_POST['service']) {
                        case "3":
                            {
                                $rshotels=$this->Enquiry->query("SELECT id, name FROM hotels where destination_id=".$_POST['destno']);
                                foreach($rshotels as $hotel){
                                        $substr.='<option value="'.$hotel['hotels']['id'].'">'.$hotel['hotels']['name'].'</option>';
                                }
                                $str.= '<select id="cmb_hotel" name="cmb_hotel">'.$substr.'</select>';
                                $str.= '<div id="divhotel_details"></div>';
                            }
                            break;
                        default:
                            break;
                    }
                    $return['error'] = true;
                    $return['msg'] = $str;
                }
            exit(json_encode($return));
    }

`

我直接把html代码贴在div里面。它很好地显示了酒店列表。但是选择“divhotel_details”的代码是什么?当我点击 divhotel_details 时,我必须再次调用 ajax 来生成该酒店的价格。

请给我建议。

提前致谢。

最佳答案

您可以在此处查看调用 json 服务的正确方法:) - https://rvieiraweb.wordpress.com/2013/01/21/consuming-webservice-net-json-using-jquery/

编辑:

<script>

function AjaxCall(){
  var hotel_val = $("#ddl_hotel").val();

   //do service ajax call passing the hotel val 

   success: function(response) {
     $("#display_info").empty();
     //this 
     $("#display_info").append(response.Yourfields);
     //or LOOP and show in div
   }, 
   error: function(response) {
       $("#display_info").append("No info for this hotel");
   }
}
</script>

<select id="ddl_hotel" onchange="AjaxCall();">
  <option value="hotel1">Hotel 1</option>
  <option value="hotel2">Hotel 2</option>
  <option value="hotel3">Hotel 3</option>
</select>

<div id="display_info">

关于php - 在 JSON 的响应对象上调用 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14478061/

相关文章:

php - 自定义错误页面Intranet子域

php - 这个简单的 php 代码有什么作用?

javascript - Google 文档的自定义键盘快捷键(更改颜色 - 背景颜色)

python - UnicodeDecodeError : 'utf-8' codec can't decode byte 0xff in position 0

java - Jackson Mixin 无法在 Pojo 到 json 中工作

php - 如何构建 'related questions' 引擎?

php - 对多对多表进行分组的 Mysql 条件

javascript - 未知脚本正在运行并在点击时重定向到未知网站

jquery - 从不选择导航栏按钮 - jquery mobile

php - 当 allday 为 false 时,FullCalendar 不显示来自 JSON 事件的时间