Rager58 Rager58 - 4 months ago 6x
PHP Question

Pass an argument on event change using http post request

I'm using the bootstrap ON/OFF switches to send an http post request on the event change. Whenever a switch changes its state from ON to OFF or OFF to ON, an http post request is triggered. Yes, it is working. However, I am not sure how to pass an argument through the http post function ($.post).

The update_state.php file takes in the argument 'state'. For example, update_state.php?state=ON will change the value of state field in the database.

The code I have so far.

<script type="text/javascript">
$('input[name="onoffswitch"]').on('switchChange.bootstrapSwitch', function(event, state) {
console.log(state); // true | false
state: "state"
function(data, status){
console.log("Data: " + data + "\nStatus: " + status);




if (!$database) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;

echo "Success: A proper connection to MySQL was made!" . PHP_EOL;

$state = $_GET['state'];

$sql = "UPDATE devices SET state='$state' WHERE id=1";

if (mysqli_query($database, $sql)) {
echo "Record updated successfully.";
} else {
echo "Error updating record: " . mysqli_error($database);




$('input[name="onoffswitch"]').on('switchChange.bootstrapSwitch', function(event, state) {
    console.log(state); // true | false
    $.post( "update_state.php", { state: state }) // here { state: state } means {parameter-name: parameter-value}
      .done(function( data ) {
        alert( "Data Loaded: " + data );