从AJAX var到PHP变量获得正确的成功代码

时间:2013-11-22 12:59:31

标签: javascript php ajax

我一直在研究这个问题,并发布了几个相关的主题,但这个问题略有不同。我有以下AJAX代码,其下面有一些html表单,#container.myselect是下拉框的类。当我更改框中的值时,我希望能够在select下面的其他字段上使用该值。 AJAX代码有点工作,警报在更改时显示正确的值,但正如您所看到的,我已经尝试了很多success函数但没有运气。最接近的是 $('#reloadtest').html(data);将在我的PHP中显示值,每次我从那时起更改值都会改变很多,但它会重新加载容器中的页面。

基本上我想知道如何重新加载数据而不是整个html /页面,所以我可以在PHP中使用下拉值。

  $(document).ready(function(){
      $('#container').on( 'change', '.myselect', function() {                               
            var orderidVal = $(this).val();
            alert(orderidVal);
              $.ajax({
                       type: "POST",
                       data: { orderidType : orderidVal },

                       success: function(data) {
                            //$('#container').reload('#container', function() {});
                            //$('#quoteselect').reload('#quoteselect', function() {});
                            //$('#container').load('orders.php #container', function() {});
                            //$('#quoteselect').load('orders.php #quoteselect', function() {});
                            //$('#testreload').reload('#testreload', function() {});
                            //location.href = "test2.php"
                            $('#reloadtest').html(data); //this allows me to use the variable but reloads the whole page within the page
                       }
            })
        });
      }); 

3 个答案:

答案 0 :(得分:0)

使用两个div - 一个可以放置ajax响应,另一个可以放入剩余的HTML代码

答案 1 :(得分:0)

它会重新加载您的页面,因为您尚未指定要请求的页面。 使用下面的代码并将/ path / to / script替换为您的实际脚本路径

$(document).ready(function(){
    $('#container').on( 'change', '.myselect', function() {                               
        var orderidVal = $(this).val();
        alert(orderidVal);
        $.ajax({
            cache: false,
            async: true,     
            type: "POST",
            url: '/path/to/script',
            data: { 'orderidType' : orderidVal },
            success: function(data) {
                $('#reloadtest').html(data);
            }
        });
    });
}); 

答案 2 :(得分:0)

如果您正在执行ajax请求,必须分配网址发送请求的位置。

如果你没有请求任何东西jQuery重新加载页面。正如您所见here in the jquery-api-docu,URL是请求的第一个也是最重要的参数。

$.ajax({
  url: "/url/to/the/php/script.php",
  success: function(data){
   //do something ... with data parameter
  }
});

将是使用javascript的ajax-request的简单方法。