1

I'm trying to accomplish one simple task (but is not simple for me).

I have a form and I'm having a trouble with this check box.

    <input type="checkbox" name="b"  
<?php if (isset($_POST[b])) echo "value='y'"; else echo "value='n'"; ?>/>

I'm not sure if I use the right one, but it doesn't work for me.

So basically I want the value of the input of b will be y if the checkbox is checked else it will always be n if the checkbox is unchecked.

8
  • That isn't how a checkbox works. The checkbox posts a value when selected or nothing at all. Perhaps you want the radio input? Commented Jun 17, 2012 at 5:24
  • are you new to web technology Commented Jun 17, 2012 at 5:24
  • Are you submitting the page to itself? Commented Jun 17, 2012 at 5:25
  • @Somebodyisintrouble yeah I forgot to put this is homework Commented Jun 17, 2012 at 5:47
  • 1
    Yes, then you would be putting the if (isset($_POST[b])) check before your insert/update query. =] Commented Jun 17, 2012 at 6:09

5 Answers 5

4

That's not how a checkbox works.

It's checked when the checked attribute is there.

<input type="checkbox" name="a" value="a" checked /> Checked
<input type="checkbox" name="a" value="a" /> NOT Checked

So you want to use

<input type="checkbox" name="a" value="a" <?php echo isset($_POST['b']) ? "checked" : ""; ?>/>

Now if $_POST['b'] is set, the checkbox will be checked.

Also, you have $_POST[b]. The b should be in quotes. It should be $_POST['b']

Sign up to request clarification or add additional context in comments.

2 Comments

well I want the value will be n if is not check and it will be y if is check` can I do the same from your code?
I'm not really understanding what you're trying to do. Are you trying to get the value of the checkbox and do something with it in your code or are you trying to literally output the attribute value="y/n" to the HTML of the checkbox? If its the latter, you can add on to the echo. echo isset($_POST['b']) ? 'checked value="y"' : 'value="n"';
2

You have to use two conditions one is for showing checked/unchecked and second for showing y/n

 <input type="checkbox" name="b" <?php echo (isset($_POST['b'])?"value='y'":"value='n'")?> 
 <?php echo (isset($_POST['b'])?"checked":"") ?>  />

Comments

0

tested code!

<form method="post">
   <input type="checkbox" name="b" <?php if (isset($_POST['b'])) echo "value='y'"; else echo "value='n'"; ?>/>
   <input type="submit" name="asd" value="asd">
</form>

So go with the following

<?php if (isset($_POST['b'])) echo "value='y'"; else echo "value='n'"; ?>

1 Comment

Well this is the same that I'm using and it doesn't work that is why I want to know.
0

This worked for me:
    first: Giving the checkbox a default value
    then: assign the desired value only if the box is checked.

    <input type="hidden" name="b" value="n">
    <input type="checkbox" name="b" value="y" >

Comments

0

*Approved by Deployment:

        <input type="radio" name="dep_approval_status" value="Approved"
            <?php 
                if ($deploy['dep_approval_status'] === "Approved")
                {
                    echo ' checked'; 
                }

            ?> 
        /> Yes, approved 

        <input type="radio" name="dep_approval_status" value="Not Approved"
            <?php 
                if ($deploy['dep_approval_status'] === "Not Approved")
                {
                    echo ' checked';
                }
            ?> 
        /> Not Approved

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.