Skip to content
Advertisement

How to send a boolean value by GM_xmlhttpRequest?

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 ?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement