简体   繁体   中英

jQuery.ajax and not parsed via GET request

I'm trying to parse value (adults) from select option field through a GET request with the use of AJAX. I can parse the value through the url - by checking the url with the alert of the url in the jQuery function. But I can't retrieve the value within the page I do the GET request.

jQuery.ajax

<script>
  $('.roomAvailable').change(
   function roomChanges(data) {
     var over = '<div id="overlaysRooms">' 
              + '<div id="loading"><div class="loaders"></div><div id="loadertextsearch">Vent Venligst...</div>' 
              + '</div>';
     $(over).appendTo('.rooms');
     $('.rooms').addClass('overlaysRooms');
     $.ajax({
       type: 'GET',
       data: {
         'arrival': '<?php echo strval($_GET[' arrival ']); ?>',
         'departure': '<?php echo strval($_GET['departure ']);?>',
         'adults': $("#adults option:selected").val(),
         'hotelId': '<?php echo strval($_GET[' hotelId ']);?>',
         'room1': $("#rooms option:selected").val()
       },
       url: '<?php echo $baseUrl ?>/hotels/hotelRoomAvailable.php',

       success: function(data) {
         alert(this.url);
         $('#roomsavailable').replaceWith(data);
         $('#overlaysRooms').remove();
       },
       error: function(xhr) {}
     })

   });

</script>

hotelRoomAvailable.php:

<?php
  var_dump(intval($_GET['adults']));
?>

Alert value: http://example.com/hotels/hotelRoomAvailable.php?arrival=03%2F13%2F2015&departure=03%2F18%2F2015&adults=5&hotelId=375477&room1=1

So when I check with var_dump to retrieve value of adults I get nothing. Why is it the value is not parsed to the page when I see the value is parsed in the alert message from jQuery ?

You are (almost certainly) getting the expected result back from the PHP script you are calling.

What if you insert, just before $('#roomsavailable').replaceWith(data); , the line alert($('#roomsavailable').length) ? – Quentin

Then I get 0 @Quentin – Troels Johannesen

The problem is, that $('#roomsavailable') doesn't find any element with that id, so there is nothing to replace with the data you get.

You need to make sure that an element with that id exists in the document.

Please just add the _GET variables to the url.

And remove the "type" and "data" variables completely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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