简体   繁体   中英

Get path from parent URL (current window URL) in ajax call looped inside another ajax call

I have a php page (client.php) contains the script below. The script performs an ajax call. Upon success, it executes another ajax call. I'm having an issue with the second ajax call where it returns the request_uri as "sql/adminloademail.php" not the parent path which is "/client.php". How do I tell php to echo the parent path "/client.php" which appears in the current window URL?

SCRIPT

$.ajax({
  method:'POST',
  url:'sql/adminaddclient.php',
  data:formData,
  contentType: false,
  processData: false,
  success: function(data){
    $.ajax({
      type:'POST',
      url:'sql/adminloademail.php',
      success: function(data){
        $('#account_list').html(data);
      }
    });
  }
});

PHP(adminloademail.php)

$page = parse_url(filter_input(INPUT_SERVER, 'REQUEST_URI' , FILTER_SANITIZE_STRING), PHP_URL_PATH);
echo $page;

You can set parent url to a variable and then send it with the data param.

var parentUrl = window.location.href;

$.ajax({
  type:'POST',
  url:'sql/adminaddclient.php',
  data:formData,
  contentType: false,
  processData: false,
  success: function(data){
    $.ajax({
      type:'POST',
      data:{"parentUrl":parentUrl},
      url:'sql/adminloademail.php',
      success: function(data){
        $('#account_list').html(data);
      }
    });
  }
});

Also you can try to get it from headers.

$headers = apache_request_headers();
if (isset($headers['Referer'])) {
    echo "Referer: " . $headers['Referer'];
}

Since PHP 5.4.0 apache_request_headers also available under FastCGI.

Since PHP 5.5.7 apache_request_headers also available in the CLI server.

http://php.net/manual/en/function.apache-request-headers.php

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