1

I have some trouble with running an action when a checkbox is checked or unchecked. (i want to run the action as soon the checkbox is checked)

I tried with an action that makes an if statement run

<input type="hidden" name="Action" value="check">
<input type="checkbox" name="vehicle" value="">  Edited<br>

if (isset($_POST['Action']) && ($_POST['Action'] == 'check')) {
    var_dump('random check');
}

I also tried with a href that links it to another page which also didn't work.

<input type="checkbox" name="vehicle" value="" href="ingevoerd.php"> Edited<br>

The goal is to run a prepared statement with an update Mysql query. Like this one

$stmt = $db->prepare('UPDATE personen SET firstname=?, lastname=?, telefoonnummer=? where id = ?');
$stmt->execute(array($firstname,$lastname,$telephonenumber,$user['id']));

I prefer using php over javascript, but i am open for some sugestions

sansactions
  • 215
  • 5
  • 17
  • 3
    Do you want to run that as soon as the checkbox is checked or are you submitting a form? As in, should the page reload or redirect before the action is run? – Mike Cluck Oct 14 '16 at 19:43
  • i want to run the query as soon as the checkbox is checked – sansactions Oct 14 '16 at 19:48
  • Then you're going to need yourself a dose of [AJAX.](https://developer.mozilla.org/en-US/docs/AJAX) It allows you to send a request to your server and process the response without reloading the page. – Mike Cluck Oct 14 '16 at 19:52
  • any sugestions? haven't used ajax alot and kinda on a deadline, thank you – sansactions Oct 14 '16 at 19:53
  • Sorry, you've just got to learn how to do it. Put your HTML in one file, your PHP in another. Perform an AJAX request to, for example, `updateperson.php` [when the checkbox state changes](http://stackoverflow.com/questions/8423217/jquery-checkbox-checked-state-changed-event). – Mike Cluck Oct 14 '16 at 20:00
  • yeah, i thought it would have to b something like that, thanks for the feedback, i really apreciate it – sansactions Oct 14 '16 at 20:11

1 Answers1

0

Use Ajax because javascript handles the browser side operations if you want to do on the server side you must need a submit button to post the data to the server.OR put this checkbox in FORM place an event in jquery on click And submit form automatically on click event

Muhammad Usman
  • 1,403
  • 13
  • 24