Jump to content


Photo

from 'on' to 1


  • Please log in to reply
3 replies to this topic

#1 mATOK

mATOK
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts
  • LocationToronto, Ontario

Posted 13 October 2006 - 07:50 PM

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?
"Not everything that counts can be counted, and not everything that can be counted counts."

W3 Web Tutorials | Check Your html | CSS layout techniques | ALA | Zen Garden | CSS Links

Navs with flavor | CSS Playground | CSS boxes

#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 13 October 2006 - 07:57 PM

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



#3 mATOK

mATOK
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts
  • LocationToronto, Ontario

Posted 13 October 2006 - 07:58 PM

thank you, I did not realize that you could set the value of a check box

so could I then use

<input type="checkbox" name="0" value="0" checked>
"Not everything that counts can be counted, and not everything that can be counted counts."

W3 Web Tutorials | Check Your html | CSS layout techniques | ALA | Zen Garden | CSS Links

Navs with flavor | CSS Playground | CSS boxes

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 13 October 2006 - 08:02 PM

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.
<?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
}
?>

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users