1

If I have a variable that needs to be posted to a PHP script without refreshing the page. Is this possible? And if so, how?

My attempt using jQuery:

$.ajax({
   url: "myphpfile.php",
   type: "post",
     data: json/array/whatever,

     success: function(){ // trigger when request was successfull
       window.location.href = 'somewhere'
     }
   })

How would I receive an array passed in my php script?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Yusuf Ali
  • 95
  • 2
  • 11

2 Answers2

1

Use GM_xmlhttpRequest() to allow for cross-domain posts (which it will be in most scenarios).

Greasemonkey Script:

// ==UserScript==
// @name     _Sending arbitrary data, demo
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant    GM_xmlhttpRequest
// ==/UserScript==

var someArray       = [1, 2, 3];
var serializedData  = JSON.stringify (someArray);

GM_xmlhttpRequest ( {
    method:     "POST",
    url:        "http://SERVER.COM/PATH/ShowJSON_PostedData.php",
    data:       serializedData,
    headers:    {"Content-Type": "application/json"},
    onload:     function (response) {
                    console.log (response.responseText);
                }
} );


ShowJSON_PostedData.php:

<?php
    echo '<title>JSON data</title>';

    echo '<h2>JSON post data:</h2><pre>';

    $jsonData   = json_decode($HTTP_RAW_POST_DATA);
    print_r ($jsonData);

    echo '</pre>';
?>


The console. will show:

<title>JSON data</title><h2>JSON post data:</h2><pre>Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
</pre>
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • ... what? You haven't provided code for `GM_xmlhttpRequest`, just usage, OP said nothing about cross-domain (which it usually *isn't* in "most scenarios"), and Greasemonkey scripts are hardly a user-friendly thing to require. – ceejayoz Dec 10 '12 at 20:13
  • 1
    @ceejayoz, `GM_xmlhttpRequest` is a ***standard*** Greasemonkey function (now linked) and **this is a Greasemonkey question!** And, yes, most such scenarios ***are*** cross domain. If the OP controlled the server, that the GM script target-page is on, he would not be asking, "how would i receive an array passed in my php script". Nor would he need to use a GM script. Now kindly remove your erroneous downvote. – Brock Adams Dec 10 '12 at 20:23
0

The accepted answer of this thread may be really useful since it shows a simple case : using jquery $.ajax to call a PHP function

My suggestion is : make something that works, then progressively add complexity until you reach your custom case. This way you feel much safer and you are aware of potential problems as they get in.

To pass an array from php to the client side, you may use echo json_encode($myArray); in your php script.

Hope this helps

Community
  • 1
  • 1
Frederik.L
  • 5,522
  • 2
  • 29
  • 41
  • 2
    any reason for the down vote and suggestions to make it better ? thank you ! – Frederik.L Dec 10 '12 at 20:41
  • 1
    @community It would be awesome to have a required field before being able to downvote. Please someone show me a single case in which you think that an answer is bad but could not explain why. – Frederik.L Dec 10 '12 at 21:24