0

Greetings Stackoverflow

How do I set the php $_GET[] array from Jquery? I have a string looking simmilar to this: $sample_string = "Hi there, this text contains space and the character: &";. I also have a variable containing the name of the destination: $saveAs = "SomeVariable";. In PHP it would look following: $_GET["$SomeVariable"] = $sample_string;.

How do I do this in Jquery?

Thanks in advance, Rasmus

4
  • Some more context would be helpful. What exactly are you trying to do? Commented Sep 21, 2012 at 13:57
  • Get the jquery version of '$_GET["somevar"] = "Some String";' Sorry for the complicated description. Commented Sep 21, 2012 at 13:58
  • 2
    In short, you're going to have to execute a GET request using AJAX on the client-side ($.get('/my/script.php', { something = "hi" }, function() { /* callback */ }) in jQuery-land). JavaScript has no direct access to your PHP variables. Commented Sep 21, 2012 at 13:59
  • @dotTutorials That's extremely vague. Are you looking to make a get request? Are you looking to access query string parameters from javascript? What's the goal you have in mind from your question? Commented Sep 21, 2012 at 14:00

5 Answers 5

2

If you're using jQuery, you'll have to set up an AJAX request on the client side that sends a GET request to the server. You can then pull the data you supplied in the request from the $_GET[] array on the server side.

$(function() {
    var data =  { 
        sample_string: "hi",
        saveAs: "something"
    };
    $.get('/path/to/script.php', data, function(response) {
        alert(response); // should alert "some response"
    });
});

And in script.php:

<?php
$sample = $_GET['sample_string']; // == "hi"
$saveAs = $_GET['saveAs']; // == "something"
// do work
echo "some response";
?>
Sign up to request clarification or add additional context in comments.

Comments

2

Can't tell if you're looking to grab a GET param from javascript or set a GET param from jQuery. If it's the former, I like to use this code (stolen a while back from I can't remember where):

var urlParams = {};
(function () {
    var match,
        pl     = /\+/g,  // Regex for replacing addition symbol with a space
        search = /([^&=]+)=?([^&]*)/g,
        decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
        query  = window.location.search.substring(1);

    while (match = search.exec(query))
       urlParams[decode(match[1])] = decode(match[2]);
})();

Then you can call

var cake = urlParams['cake'];

To get the $_GET param specified by http://someurl.com?cake=delicious

If you want to send a $_GET parameter, you can use either jQuery's $.get() or $.ajax() functions. The $.get function is more straightforward and there's documentation on it here http://api.jquery.com/jQuery.get/

For $.ajax you would do something like this:

var trickystring = "Hi there, this text contains space and the character: &";
$.ajax({
  url:'path/to/your/php/script.php',
  data: {
    'getParam1':trickystring,
    'getParam2':'pie!'
  },
  type:'GET'
});

Now in PHP you should be able to get these by:

$trickystring = $_GET['getParam1'];
$pie = $_GET['getParam2'];

Hope these examples GET what you're looking for. (Get it?)

Comments

1

if $sample_string is what you want in jquery, you can do var sample_str = '<?php echo $sample_string; ?>'; and then use the js variable sample_str wherever you want.

Now, if you want to do set $_GET in jquery, an ajax function would be way to go.

 $.ajax({
 url:'path/to/your/php_script.php',
 data: {
'param1': 'pqr',
'param2': 'abc'
},
 type:'GET'
});

Comments

0

Do you mean that would look like $_GET[$saveAs] = $sample_string y think. $_GET is a variable for sending information from a page to another by URL. Its nosense to do it in jQuery that is not server side. If you want to dynamically set the $_GET variable to send it to another page you must include it in the URL like:

/index.php?'+javascriptVariable_name+'='+javascriptVariable_value+';

2 Comments

Exactly, however; if the string contains the character '&' this techinque will not work.
Yes, its true. But you can transform '&' character in javascript to another valid character/s and then decode it in php in the other page.
-1

$_GET is just a URL parameter. So you can access get like /index.php?id=1:

echo $_GET['id'];

Look at this article, it shows all the ways to load stuff with ajax:

http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

1 Comment

I was getting to that! Give me a sec.

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.