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!

Community
  • 1
  • 1
Phil
  • 10,948
  • 17
  • 69
  • 101

4 Answers4

2
$.ajax({
    url: "myScript.php",
    data: "userid=<?php echo intval($_GET['userid']); ?>",
    success: function( data ) {
        $('#myDiv').html( data );
    }
});
genesis
  • 50,477
  • 20
  • 96
  • 125
  • Is that a safe/best practice type way of doing it? Does jQuery have a function to pull data from the url? – Phil Jul 26 '11 at 15:52
  • @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. – a1ex07 Jul 26 '11 at 15:54
  • This solution does not work. You can't have `PHP` in a `JS` script. – Phil Jul 26 '11 at 15:56
  • @Phil: did you try it? Delete your downvote and try it first -.- – genesis Jul 26 '11 at 16:39
  • I meant to remove it, but I can't because you didn't edit it. It isn't the solution, but that doesn't mean you should get a downvote... – Phil Jul 26 '11 at 16:57
  • @Phil:I think this 100% is solution! Just try it and you'll see! – genesis Jul 26 '11 at 17:04
  • 1
    I want to keep the `PHP` and the `JS` separate. You can't do that with your current solution. – Phil Jul 26 '11 at 17:13
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?

Community
  • 1
  • 1
David Nguyen
  • 8,368
  • 2
  • 33
  • 49
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']);

fire
  • 21,383
  • 17
  • 79
  • 114
  • 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. – Phil Jul 26 '11 at 16:10
  • 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 http://plugins.jquery.com/project/query-object – fire Jul 26 '11 at 16:21
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.

fire
  • 21,383
  • 17
  • 79
  • 114
bstakes
  • 1,898
  • 14
  • 12