0

I have a problem with form in Greasemonkey . I want to send a boolean value usign GM_xmlhttpRequest, but if I send:

GM_xmlhttpRequest({
  method: "POST",
  url: "http://localhost/test.php",
  data: "confirm=true",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  onload: function(response) {
    console.log(response.responseText);
  }
});

Test php:

var_dump( $_POST );

At the console I see:

array(1) { ["confirm"]=> string(4) "true" }

How can I solve this problem ?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
New One
  • 3
  • 1
  • 1
    Your server side will need to interpret the value and convert it into the data type you want. All data passed to the server comes in as a string. – Will P. Dec 22 '16 at 18:54

2 Answers2

1

Just convert the value to Boolean on the server side - you already have the value.

You can either do a straight $myVar = $_POST["confirm"] === "true";

or use filter_var with the FILTER_VALIDATE_BOOLEAN flag, to cover more options:

$myVar = filter_var($_POST["confirm"], FILTER_VALIDATE_BOOLEAN); - this allows you to cover true, TRUE, on, yes, etc. - all interpreted as Boolean true.

Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
0

Since you're sending the data in as JSON, all you need to do is decoding the post value similar to:

$var = json_decode($_POST['some_param']);

This way you'll get the correct type.

Salih Kavaf
  • 897
  • 9
  • 17