1

Disclaimer

I have searched for duplicates, but I can't seem to find them. I am surprised because this seems to be a big issue. I most likely am missing something big though.

Problem/Question

I am having the userid passed into through the url via php, myOtherScript.php?userid=1. How can I get that variable to be passed via ajax so that I may query the database with that userid, echo it out and return it to the page?

This is in global.js file

jQuery

$.ajax({
    url: "myScript.php",
    data: "userid="  - This is what I need: $_GET['userid'] - ,
    success: function( data ) {
        $('#myDiv').html( data );
    }
});

Solution

WIth the help of bstakes and this answer, I was able to figure it out with this function: (top answer)

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if(results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

Thanks for the answers guys!

4 Answers 4

2
$.ajax({
    url: "myScript.php",
    data: "userid=<?php echo intval($_GET['userid']); ?>",
    success: function( data ) {
        $('#myDiv').html( data );
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

Is that a safe/best practice type way of doing it? Does jQuery have a function to pull data from the url?
@Phil: The safe practice is to use sessions, store user_id (and maybe some other data) in $_SESSION when user successfully logs in, and get it from there.
This solution does not work. You can't have PHP in a JS script.
@Phil: did you try it? Delete your downvote and try it first -.-
I want to keep the PHP and the JS separate. You can't do that with your current solution.
|
1

You could also try using the search attribute of the window.location object.

If the url is http://www.mysite.com/display.php?userid=7 window.location.search will return "?userid=7". You will obviously need to remove the leading "?", but be aware that if there are additional GET paramaters, separated with ampersand '&', those will be included as well.

So, with a bit of additional Javascript, you can split on the '&', which gets you an array of "key=val", then you can spilt on the equal sign and create an object with {key : val}. Then you could use that object to access the query string params.

var qs = window.location.search.substring(1),
    pieces = qs.split('&'),
    i,
    qsObj {},
    tmp;
for ( var i in pieces ) {
   tmp = pieces[i].split('=');
   qsObj[tmp[0]] = tmp[1];
}

See https://developer.mozilla.org/En/Window.location for additional information on the window.location.

Comments

1

If you want to keep the JS seperate, put it in a function that accepts the user id...

function do_something(user_id) {
    $.ajax({
        url: "myScript.php",
        data: "userid=" + user_id,
        success: function( data ) {
            $('#myDiv').html( data );
        }
    });
}

Then just call do_something($_GET['user_id']);

2 Comments

This only works if you bring the function in the .php script. I want to be able to keep it in a separate JS script and still retrieve the $_GET vars.
Well I wouldn't recommend doing it this way but in javascript you can pick out the query strings using good old location.href. There's some jquery plugins to make this easier, try plugins.jquery.com/project/query-object
1

You might have to move the script inline on the PHP file then you echo out the $_GET['userid'] in the data area of your ajax call.

just found this: how to get GET and POST variables with JQuery?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.