Jump to content


Photo

HTML Checkboxes and PHP


  • Please log in to reply
3 replies to this topic

#1 tleisher

tleisher
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 09 September 2006 - 04:43 AM

Is there anyway to setup an HTML form so that if the box isn't checked it returns no, or false? RIght now if it isn't checked it just returns blank/not set, but if it is checked then it returns "On" I'd like to change this to either be checked = "yes" and unchecked = "no" or just true/false, or even on/off.

#2 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 09 September 2006 - 05:16 AM

It does return false, if you check the value in an if statement and it is unchecked, it will not go into the if statement. 

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 September 2006 - 06:59 AM

copy and past so you understand the concept of if and checkbox.

good luck.

example fully tested.

test.php
<?php

$like=$_POST['like'];

if($like=="yes"){

echo "<b>I like redarrow</b>";


}elseif($like=="no"){

echo "<b> I dont like redarrow </b>";


}
 
?>

<html>
<body>
<form method="POST" action="">

<br>

do you like redarrow

<br>

<input type="checkbox" name="like" value="yes" >yes
<br>

<input type="checkbox" name="like" value="no" >no

<br>

<br>


<input type="submit" name="submit" value="ansaw">

</form>
</html>
</body>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 September 2006 - 02:35 PM

If you take redarrow's suggestion, both checklboxes could be checked, but only one value will get passed back to your script. Most likely the value of the last box, "no", but, I believe, that's not guarenteed. If you want to do it this way, use radio buttons, which act like toggle switches.
<?php
if (isset($_POST['submit'])) { //only check if the form has been submitted
    echo 'The answer to the question is ' . $_POST['test'] . "<br>/n";
}
?>
<form method="post">
Are you awake?  Yes: <input type="radio" name="test" value="yes" checked> | No: <input type="radio" name="test" value="no"><br>
<input type="submit" name="submit" value="Answer the question">
</form>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users