简体   繁体   中英

How to get parameter from URL in PHP but after load page by jQuery?

I need get (date) parameter form URL by php but after load "step-02.php" by .load jQuery.

For example:

http://localhost:8000/selectRoom?hotelID=100&date=2016-11-08&night=5

I get the 2016-11-08 value from URL by php is $_GET['date'] but after I load step-02.php with jQuery this code $_GET['date'] does not work.

$(function () {
   $('#Step-01').hide();
   $('#Step-02').show();
   $('#Step-02').load('step_02.php');
})

HTML in step-02.php :

<div class="alert alert-success">
   // This line gives an error and not work!
   <?php echo $_GET['date']; ?> 
</div>

http is stateless so unless you pass the date back in the ajax request, or save it to the session, the server has no way of remembering what you've told it previously.

Put this in step1.php somewhere

<input type="hidden" id="date-input" value="<?php echo $_GET['date']?>" />

Then do this:

$(function () {
  var date = $('#date-input').val();
  $('#Step-01').hide();
  $('#Step-02').show();
  $('#Step-02').load('step_02.php?date='+date);
})

if i understand well at first you call this url , with these parameters : http://localhost:8000/selectRoom?hotelID=100&date=2016-11-08&night=5 , that is why in your PHP file you can get the parameters , $_GET['hotelID'] , $_GET['date'] and $_GET['night'] .

When you call step_02.php

The url does not change to a new one , it remains as it was : http://localhost:8000/selectRoom?hotelID=100&date=2016-11-08&night=5 , and that is the problem! The php file step_02.php , did not get any parameters from the client on the server request , so it does not recognise any parameter , although they are on the url.

Solutions:

A. You can get the url by : $_SERVER['HTTP_REFERER'] , which is going to give you all the url as it is, and by explode("?", $_SERVER['HTTP_REFERER']) you are going to get the part of the url that has the parameters, after that you can use explode again..

B. you can pass the parameters you wish while you execute the ajax call to the server, and get them with $_GET['parameter' ], like so:

data = {'hotelID':100,'date':2016-11-08,'night':5};
        $.ajax({
            cache: false,
            url : url,
            type: "GET",
            data : data,
            beforeSend: function( xhr ) {
                //load your spinner img
            },
            success : function(data, textStatus, jqXHR){
                //get response from server
            },
            error : function(xhr, ajaxOptions, thrownError){
                alert(thrownError);
            }
        });

OR if it need be with load():

$('#Step-02').load('path', 'key=value');

Hope helps, good luck.

Your question is not clear but I assume that you want to get the date value after load.

Use callback()

$('#Step-02').load('step_02.php', function(response, status, xhr) {
  alert( "Load was performed." );
  console.log(response);
});

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