Manixat Posted December 26, 2012 Share Posted December 26, 2012 (edited) Heya, I'm currently brainstorming this little issue of mine, I have my page right, immediately I'm running an ajax request to call a php script but I can't seem to get the URL variables from the actual URL. Is there any way I can access the original $_GET array from the called script ? ( Without having to send the variables again in the request ) One way I can do this is via session, but is there an ever easier way? Edited December 26, 2012 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/272376-access-url-variables-via-ajax-request/ Share on other sites More sharing options...
lemmin Posted December 26, 2012 Share Posted December 26, 2012 You can send the GET variables in your AJAX call. In Javascript, location.search should contain the current GET query string. You can simply append that string to your AJAX call, or parse out the ones you want. Quote Link to comment https://forums.phpfreaks.com/topic/272376-access-url-variables-via-ajax-request/#findComment-1401344 Share on other sites More sharing options...
Manixat Posted December 26, 2012 Author Share Posted December 26, 2012 Yes, that is an obvious solution, but I am calling this script from every single page on my site and that means I have to go and hardcode all of this into the ajax calls, and if I have to change anything in future that would again mean I have to change everything by hand ( which is actually the same problem with using session ), I was hoping there was another way to access the URL variables from the external script :? Quote Link to comment https://forums.phpfreaks.com/topic/272376-access-url-variables-via-ajax-request/#findComment-1401345 Share on other sites More sharing options...
lemmin Posted December 26, 2012 Share Posted December 26, 2012 Your PHP script that generates the page, the generated HTML, and your AJAX PHP script are all separate entities. The only communication between them is what information is explicitely sent to each one on an individual basis. I think your initial idea of a session is the best solution to your problem. A session would supersede the need to pass variables between one another. The only information your AJAX script would receive aside from what you pass it would be headers sent from the browser. The 'referer' header might hold the GET variables you are looking for, but these are unreliable since not all browsers are the same and the headers can be easily spoofed. Quote Link to comment https://forums.phpfreaks.com/topic/272376-access-url-variables-via-ajax-request/#findComment-1401350 Share on other sites More sharing options...
Manixat Posted December 26, 2012 Author Share Posted December 26, 2012 Well I guess there is no other way, thank you for your replies, and I shall be getting to work! Quote Link to comment https://forums.phpfreaks.com/topic/272376-access-url-variables-via-ajax-request/#findComment-1401355 Share on other sites More sharing options...
Jessica Posted December 26, 2012 Share Posted December 26, 2012 Your PHP page that outputs HTML can simply echo the $_GET array on the end of the Ajax call. Wherever you print the URL to request via Ajax, add a loop that cycles through $_GET, or even just serialized it. Quote Link to comment https://forums.phpfreaks.com/topic/272376-access-url-variables-via-ajax-request/#findComment-1401362 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.