-1

I have this code that is too generate more data from the database once the user reaches the bottom of the previously loaded content, but I need to pass the get variable from the url to the php script in order to grab the proper user content. The get variable is the user id, which used to load their page for their self and others. I know I could just store the variable in an element, but is there a better way?

Maybe I am looking at this wrong and I need some other way to access the content? I feel that I should be able to just pass the variable using the x.php?num=value, but I cannot figure out how to get that value other then placing it in an element to grab out. Does this pose any problems handling the variable this way (it is already global to everyone).

FGF
  • 21
  • 3
  • So your question is how to extract a value from the URL in JavaScript? – Felix Kling Nov 09 '15 at 21:13
  • 1
    The user ID seems like something you'd store in the session on the server and don't pass around where it can be modified. – jeroen Nov 09 '15 at 21:15
  • @felix Yes, Though I may have just found a way to solve this through saving the current page as a session variable every time you visit a new page. This would allow access during the Ajax call. I would be interested in knowing if extracting the variable is possible though. – FGF Nov 09 '15 at 21:16
  • @jeroen - what if it;s an admin account making changes to a user.. session wil already hold the admin uid.. – I wrestled a bear once. Nov 09 '15 at 21:16
  • 1
    How to get a value from the URL is a duplicate of [How to get the value from the URL parameter?](http://stackoverflow.com/q/979975/218196). Please use the search before you ask a new question. – Felix Kling Nov 09 '15 at 21:18
  • @jeroen The id can't really be modified on the site I just mean I don't want someone passing phony ids to my script, but this is a dumb worry since I know that people can do that whenever to try and break the site and find holes. – FGF Nov 09 '15 at 21:20
  • @FelixKling Sorry for the duplicate I was litterally looking for the wrong terms this entire time. I have spent about 1 hour looking, as soon as you said extract and threw the word parameter at me. I realized I missed key words. I also realized how hard research is after 30 hours with no sleep. – FGF Nov 09 '15 at 21:39

1 Answers1

0

If your URL is "x.php?num=value", then x.php will be able to access "num" like this.. $_GET['num'];

EDIT...

To get it from javascript..

function get(name){
   if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
      return decodeURIComponent(name[1]);
}

from here: How to get "GET" request parameters in JavaScript?

Community
  • 1
  • 1
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • I didn't down vote but I'm pretty sure he wants to know how to get it via javascript, not via PHP. – Leeish Nov 09 '15 at 21:18
  • The x.php?num=value was an example of passing the value in the url of the ajax call, but that doesn't matter now. Thank you for the answer. – FGF Nov 09 '15 at 21:40