-1

I'm trying to set up a webpage with Jquery that will receive button clicks from the user, pass those click values to a PHP script, that will then publish them to a MQTT broker. My connection to the broker seems to be working. I'm having problems passing variables from JavaScript to PHP. What am I doing wrong?

Here's my JavaScript:

<script>
$(document).ready(function(){
  $("#button01").click(function(){$.post("post.php", {testvalue:test01});});
});
</script>

and here is my PHP:

<?php
require("../phpMQTT.php");

$testvalue = $_POST['testvalue'];

$mqtt = new phpMQTT("192.168.1.20", 8000, "client"); 

if ($mqtt->connect()) {
    $mqtt->publish("hello/world","$testvalue",0);
    $mqtt->close();
}
?>
German
  • 10,263
  • 4
  • 40
  • 56
user3147697
  • 91
  • 1
  • 9

1 Answers1

1

You pass invalid JSON object to $.post() method. It should be:

{testvalue:"test01"}

So your JavaScript code should look like:

$(document).ready(function(){
    $("#button01").click(function(){$.post("post.php", {testvalue:"test01"});});
});

Or if test01 is variable, it should be defined first.

Please, next time look at console in your browser and check if there is no errors and if the ajax call is sending correctly.

marian0
  • 3,336
  • 3
  • 27
  • 37