2

My code checks the value of 2x checkboxes to equal true/false depending on a radio field. I see the checkboxes being ticked on the web page.

However, once they are passed over a HTML POST form, they have no values and always equal false.

If I give the checkboxes a value "TRUE" then of course they have only that value.

What am I missing here?

 <script> $(function() { var MAIN= $("input[type='radio']"); var marketingPhone = $("input[type='checkbox'][name='marketingPhone']"); var marketingRobo = $("input[type='checkbox'][name='marketingRobo']"); MAIN.on('change', function() { if ($(this).val() == "TRUE") { marketingPhone.prop('checked',true); marketingRobo.prop('checked',true); } else { marketingPhone.prop('checked',false); marketingRobo.prop('checked',false); } }); }); </script> 

The fields are as follows:

 <input type="checkbox" name="marketingPhone" value=""/> <input type="checkbox" name="marketingRobo" value=""/> 
1
  • If you put some value in checkbox than you will get it to backend. Commented Dec 6, 2017 at 12:33

2 Answers 2

2

Checkboxes, if not checked, do not get posted at all with the form.

So if you post a form with checkboxes, even if the checkboxes have some value but are not checked, the checkbox values will not be present in the $_POST or $_GET arrays.

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

8 Comments

Hi. Thanks for your input. However, if I see the chekcboxes DO get checked on the webpage, then what else do I need to do with them to have a value? The script checks them based on radio button, but these do not post a value it seems.
Sorry but I am confused here, you do not put any value in checkboxes. So even they would be checked, you can not get their value.
Oh. Apologies. I thought the "checked" is read as the value. How can I pass the value to the checkbox with my method, if it's possible at all?
You should put values whatever you want to your checkboxes. And they will be posted with the form only if they will be checked. If not then you can not get even the names of the checkboxes in $_POST or $_GET array keys. So if marketingPhone is not checked and you check echo $_POST['marketingPhone'] it will throw undefined index. Hope this is clear to you now.
In short, you give value to value attributes to checkboxes and if you check them, you will get them in php code else not.
|
1

You need to specify a value in the HTML. If a checkbox is checked, that value is posted. These values can be anything and are definitely not the same as the true/false that are used for the prop()-call (those just mean 'set or clear the check mark').

Example:

<input type="checkbox" name="marketingPhone" value="yes"/> <input type="checkbox" name="marketingRobo" value="absolutely"/> 

When both are checked, this is posted:

marketingPhone=yes&marketingRobo=absolutely 

When not all are checked, the corresponding name=value are omitted from the posted data.


If you really want to post a value (e.g. "true" or "false") for every checkbox all the time, then the only way to do so is by adding hidden fields (one for each checkbox) that you control yourself, setting the value of the hidden field to "true" or "false" using script. You might as well then not set the checkbox value to prevent confusion on the receiving end.

Also be aware that form fields always post strings, so either treat them as literal strings or parse them into booleans on the receiving end.

1 Comment

Hi Peter, thanks for the input. Is it possible to post 'true' or 'false' as checkbox values that correspond to the prop-set values? Thing is, if I add value as true to the checkbox level, then only true is posted, no matter how the prop is set.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.