Jquery设置PHP $ _GET数组

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

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";
?>

$_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/

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'
});

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?)

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+';