php - simple yes or no checkbox for forms -


i have inserted checkbox in form.

my code:

<input type="checkbox" id="checkbox" name="checkbox" value="1"/>  if($checkbox = ($_post['checkbox']) == '1')         {             $checkbox = "si";         }         else         {             $checkbox = "no";         } 

i if checkbox checked receive "yes" otherwise "no". thanks.

you've written wrong if condition here, cannot use assignment in conditions.

also there no need assign value variable in checking condition, can directly use $_post['checkbox']. this,

if($_post['checkbox'] == '1') {    $checkbox = "si"; } else {    $checkbox = "no"; } 

update:

a better option use isset() determine if variable set , not null. this,

if(isset($_post['checkbox'])) {    $checkbox = "si"; } else {    $checkbox = "no"; } 

program go in if condition when user has checked checkbox. in above case value attribute <input> not required. html this,

<input type="checkbox" id="checkbox" name="checkbox"/> 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -