Jump to content

Recommended Posts

Posted

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.

Posted

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>

 

 

Posted

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.

Posted

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>

 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...