Jump to content

from 'on' to 1


mATOK

Recommended Posts

Hey there, I have a form full of checkboxes that sends its input to an array.

I know that if a box is checked then the array reads 'on', otherwise I assume it will containe 'off'

I'd like to know how I can convert all of the on's to 1's and the off's to 0's

would I use strcmp for this or is there a built in php function?
Link to comment
https://forums.phpfreaks.com/topic/23892-from-on-to-1/
Share on other sites

if the box is checked it will give whatever value you tell it to give

<input type=checkbox name=check1 value=heyheyhey>

also realize if a box is not checked it will not pass anything. So if you try to echo out a checkbox than was not checked you will get an error.

Ray

Link to comment
https://forums.phpfreaks.com/topic/23892-from-on-to-1/#findComment-108584
Share on other sites

no problem. Also I forgot to add, you can suppress the error by adding an @ in front of the variable.

Here is a quick example.
[code]<?php
if(isset($_POST['submit'])){
echo @$_POST['check1']."---".@$_POST['check2'];
} else {
?>
<form name=form method=POST action="">
  <input type=checkbox name=check1 value=3><br>
  <input type=checkbox name=check2 value=1><br>
  <input type=submit name=submit value=submit>
</form>
<?php
}
?>[/code]

Now if you only check one box you will just get a blank for the box that was not checked. If you take away the "@" in front of the $_POST variables you will get an error for the box that was not checked.

Just a little FYI for ya

Ray
Link to comment
https://forums.phpfreaks.com/topic/23892-from-on-to-1/#findComment-108588
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.