desbest Posted October 15, 2017 Share Posted October 15, 2017 How do I get the value of a checkbox POST data? Here is my code. <?php if($input->post->changesettings) { // $report->of(false); echo "<h1>deactivated is: <u>".$input->post->deactivated."</u>!</h1>"; $user->first_name = $input->post->first_name; $user->deactivated = $input->post->deactivated; } ?> <form method="POST" action=""> <input type="text" style="width: 390px; height: 48px; font-size: 2em;" class="mediumtext required styleme" name="first_name" value="<?=$user['first_name'];?>"> <input type="checkbox" name="deactivated" value="<?=$user['deactivated'];?>"> <input type="submit" name="changesettings" value="Change settings" class="bigbutton" /> </form> It gets the value of the "first_name" text box, but not the "deactivated" checkbox. What is going on? What am I doing wrong? When printing the deactivated in the h1 tag, it always shows 0 when checked and submitted. Link to comment Share on other sites More sharing options...
abdus Posted October 15, 2017 Share Posted October 15, 2017 It works fine in my setup. Do you have any JS modifying the form? Also, check if you've mistyped ($input->post->deactivated == $something) as single = somewhere, rather than double == or triple === ? <?php namespace ProcessWire; if ($input->post->changesettings) { $checked = $input->post->deactivated; // checked == 1 } ?> <form method="POST" action=""> <input type="text" name="first_name" value="<?= $user['first_name']; ?>"> <input type="checkbox" name="deactivated" value="1"> <input type="submit" name="changesettings" value="Change settings" class="bigbutton"/> </form> Link to comment Share on other sites More sharing options...
Macrura Posted October 15, 2017 Share Posted October 15, 2017 at least in terms of usage in the api, checkboxes always have the value of 1, so you don't need to manipulate the value, just the checked status of the input. Link to comment Share on other sites More sharing options...
desbest Posted October 17, 2017 Author Share Posted October 17, 2017 I have found a fix for this. In HTML, whenever a checkbox is checked and is POSTed it has a value of "on". If the checkbox is unchecked it has no value. Here is the working code. <?php if($input->post->changesettings) { $deactivated = (isset($input->post->deactivated)) ? 1 : 0; $user->deactivated = $deactivated; } ?> <form method="POST" action=""> <input type="checkbox" name="deactivated" <?php if ($user['deactivated'] == 1) echo "checked='checked'"; ?>> <input type="submit" name="changesettings" value="Change settings" class="bigbutton" /> </form> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now