0

I have a quick question but I seem to be having trouble locating the answer. (possibly my wording) but here goes.

  • I was able to echo POST data from a form for the text/number variables without a hitch! but I am running in to a brick wall over these checkbox values.
  • I know that if you POST a checkbox it will post a value of "on" but lets say on the form if checkbox1 is checked, how to I make a corresponding checkbox on the POST page load already checked?
  • I know in html all you have to do is simply write "checked" within the code so I tried using an if statement to get the job done but I couldn't seem to get it working.

Any help is much appreciated!

Thanks!

Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
LuckoftheLefty
  • 75
  • 1
  • 1
  • 8

3 Answers3

2

HTML:

<input type='checkbox' name='selected' value='1'>

PHP:

$selected = isset($_POST['selected']) && $_POST['selected']  ? "1" : "0";

This code will set selected checkbox with value 1 in variable $selected and 0 if not selected.

Sagar Naliyapara
  • 3,971
  • 5
  • 41
  • 61
0

check box name will be available in post only if it is checked.

vinay rajan
  • 351
  • 4
  • 9
0
        if (isset($_POST['myCheckbox'])) {
          $checkBoxValue = "yes";
        } else {
        $checkBoxValue = "no";
        }

send checkbox value in PHP form

Community
  • 1
  • 1
kailash sharma
  • 356
  • 1
  • 12