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
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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.