Jump to content

HTML Checkboxes and PHP


tleisher

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/20201-html-checkboxes-and-php/
Share on other sites

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

good luck.

example fully tested.

test.php
[code]
<?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>
[/code]
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.
[code]<?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>
[/code]

Ken

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.