I'm trying to make a series of checkboxes in a web form that will post either "true" or "false" to the next php page, depending on whether they are checked. I decided that for each checkbox (value="true"), I would have a hidden checkbox (value="false") that would always be the opposite (when checkbox is checked, hidden checkbox is unchecked etc.) I am using jQuery to do this.
The number of 'td' elements is unknown (the 'td' can be cloned an infinite number of times using a button, to submit multiple values). I added the alerts to the jQuery for testing purposes.
When I added the jQuery code and hidden checkbox into my source code (I made it visible for testing purposes), the code wouldn't work AND all the other jQuery on my web page stopped working!
jQuery code:
$(document).ready(function(){
/***post all 'required' checkboxes instead of just checked ones***/
$(".required").click(function(event){
event.preventDefault();
alert("test");
nextIndex = this.index() + 1;
alert(nextIndex);
$(".required:eq(nextIndex)").attr("checked", !.required:eq(nextIndex).attr("checked"));
});
});
HTML:
<td>
<input type="checkbox" class="required" name="required[]" value="true">
<input type="checkbox" class="required" name="required[]" value="false" style="display: none;" checked>
</td>
You don't have to hide one of the checkboxes. Here is a solution that should accomplish what you want:
<input type="hidden" name="required" value="false" />
<input type="checkbox" name="required" value="true" />
If the checkbox is not checked, then the hidden field will be submitted with the false value. If the checkbox is checked, then the true value from the checkbox will override the false value from the hidden input.
As long as you can name each checkbox something slightly different, it should work.
The following should work as well for an array of values:
<input type="hidden" name="required[0]" value="false" />
<input type="checkbox" name="required[0]" value="true" />
<input type="hidden" name="required[n]" value="false" />
<input type="checkbox" name="required[n]" value="true" />
This way, you don't need any client side javascript to toggle the hidden checkboxes.
This line is probably causing things to break:
$(".required:eq(nextIndex)").attr("checked", !.required:eq(nextIndex).attr("checked"));
You probably meant:
$(".required:eq(" + nextIndex + ")").attr("checked", !$('.required:eq(' + nextIndex + ')').attr("checked"));
Also note that you need to take nextIndex out of the quotes.
My guess is that your javascript console would have thrown some sort of error related to this.
EDIT:
Another error that I noticed was this:
nextIndex = this.index() + 1;
.index() is a jquery function which must be called on a jQuery object. this is the element that was clicked, so you will need to say:
nextIndex = $(this).index() + 1;
BTW, you might want to check out jQuery's .next() function. It would save you from all of this nextIndex mumbo jumbo.
If you are naming your checkboxes like this, you don't necessarily have an index to work with:
<input type="checkbox" name="checkbox_name[]" />
Then this snippet of jQuery should work for you, assuming that you are OK with relying on javascript.
$('form').submit(function(){
$(":checkbox:not(:checked)").each(function(element, index){
$(this).attr({value: 'false', checked:'checked'});
});
return true;
});
This loops through all of the unchecked checkboxes, checks them, and sets their value to false. Here is what you get in PHP:
[checkbox_name] => Array
(
[0] => off
[1] => on
[2] => off
)
Instead of:
[checkbox_name] => Array
(
[0] => on
)
Related
I have a html form
<form action="process.php" method="post">
<input type="checkbox" name="name[v1]" />
<input type="checkbox" name="name[v2]" />
<input type="checkbox" name="name[v3]" />
<input type="submit" name="update" value="update">
</form>
If they is only one check box is ticked then I only see that check box
Array
(
[\'v3\'] => on
)
If I have checked all three box then I see them all.
Array
(
[\'v1\'] => on
[\'v2\'] => on
[\'v3\'] => on
)
Is they any way I can see all of my checkbox even if they are not checked.
process.php
foreach( $_POST['name'] as $k => $v )
{
echo "key: ".$k;
}
Checkboxes and radio buttons are not passed on to the processing script if they don't have a "checked" attribute set. This is HTML4 by design.
The only way you can set a state is using something like:
if(!isset($_POST['mycheckbox'])){ $_POST['mycheckbox'] = 0; }
or better yet:
$_POST['mycheckbox'] = isset($_POST['checkbox']);
Regarding radio buttons, you should only use the first version since radio buttons can have more than one value so instead of setting a TRUE/FALSE in them, you want to set a default value instead.
Another note, DISABLED elements are not posted, even if they have a value, you will never see them, this is another design feature of HTML4+
I'm working on a page redesign, and I've added 6 different buttons for the 6 options a user can choose instead of 6 radio boxes & 1 button. I just need to adjust the code to make sure that on the next page, the right total and text is displayed based on what button is pushed.
I just need some of the text on the next page to change depending on which of the buttons is pressed.
Here is what was there originally:
<input id="diamondmonthly" class="radio" type="radio" value="diamondmonthly" name="membership">
<input id="diamondyearly" class="radio" type="radio" checked="checked" value="diamondyearly" name="membership">
<input id="c4" class="radio" type="radio" value="c4" name="membership">
<input id="c3" class="radio" type="radio" value="c3" name="membership">
<input id="c2" class="radio" type="radio" value="c2" name="membership">
<input id="c1" class="radio" type="radio" value="c1" name="membership">
<a id="btnSave" class="xlarge button-submit" onclick="$j('#btnSave').attr('disabled', 'disabled');qc.recordControlModification('btnSave', 'Enabled', '0'); qc.pB('MembershipForm', 'btnSave', 'QClickEvent', '');" href="#">
<script>
//<![CDATA[
qc.registerForm(); qc.imageAssets = "http://images.comfiles.com/assets/images"; qc.cssAssets = "http://images.comfiles.com/assets/css"; qc.phpAssets = "/assets/php"; qc.jsAssets = "http://images.comfiles.com/assets/js"; qc.regCA(new Array("btnSave","btnSave2","c1","c2","c3","c4","diamondyearly","diamondmonthly")); jQuery(document).ready(function($j){if($j.isFunction($j().tooltip)){$j("#MembershipForm :input[data-error]").tooltip({ position: "center right", tipClass: "error_tooltip", offset: [0, -4] })}});
//]]>
</script>
This can be dealt with using a listener one each button, or by delegating the work to a single ancestor element. In this case, it's probably easiest to put a single click listener on the form. When it's called, you can use the associated event object to see where it came from and react accordingly.
Note that when getting form controls by name, if only a single control has that name then only a single element is returned. If two or more controls have the name, then a HTMLCollection of the elements with that name is returned.
The listener can be something like:
<form onclick="handleClick(this)" ...>
And the listener (untested but you should be able to get it going from here):
function handleClick(form) {
var button, buttons = form['membership'];
// Now show text depending on which membership button is checked
for (var i=0, iLen=buttons.length, i<iLen; i++) {
button = buttons[i];
// Assuming that the text element to display has an ID
// matching the value of the checked button
if (button.checked) {
document.getElementById(button.value).style.display = '';
} else {
document.getElementById(button.value).style.display = 'none';
}
}
}
The script will run every time there is a click anywhere in the form, so the right text will always be displayed, even if the reset button is clicked.
The if..else can be replaced by:
document.getElementById(button.value).style.display = button.checked? '' : 'none';
but the longer version may be clearer.
Oh, and get rid of the CDATA delimiters in the script element, they are only required for documents that are served as XML.
I am doing this :
function GETVALUE()
{
if(document.getElementById("requirenewpage").checked == true)
{
document.getElementById("requirenewpage").value;
var cval= parseInt(document.getElementById("requirenewpage").value);
}
}
HTML-----------------------------
<input type="checkbox" id="requirenewpage" unchecked value= "GETVALUE();" >
I need to insert into a mysql table , 0 or 1, which is taken from the VALUE attribute of the checkbox.....but am not able to do it...please help???
Its always inserting 0 into the database, albeit am setting the value as 1 in the function GETVALUE().....
Actually you don't need any of this i think. You can use this html:
<input type="checkbox" id="requirenewpage" value= "1" >
and the checkbox will send a value of 1 to the server if checked, otherwise it won't send anything (and the corresponding $_POST['requirenewpage'] or $_GET['requirenewpage'] won't be set).
If the checkbox is checked it's value is sent to the server and a key in the $_POST array (if you use POST) is created with the name of the checkbox and the value of hte checkbox.
you can do, serverside:
$chkboxval = 0;
if (isset($_POST['requirenewpage'])){
$chkboxval = $_POST['requirenewpage'];
}
I'm shocked that nobody has answered this correctly yet...
Change the checkbox to the following:
<input type="checkbox" id="requirenewpage" name="requirenewpage" value= "1" />
The ID of an input element is used for script access and styling only, if you want to submit the element in a form it must have a name attached to it.
You are wrong with your html. Change your checkbox code from
<input type="checkbox" id="requirenewpage" "unchecked" value= "GETVALUE();" >
to
<input type="checkbox" id="requirenewpage" onclick= "GETVALUE();" >
I have a php array which I'm trying to get jQuery to check those checkboxes, but I can't seem to get it working. My php array name is "toCheck" which is:
Array
(
[0] => 0
[1] => 3
[2] => 4
)
0,3,4 are the checkboxes I need checked. Here's my checkboxes:
<input type="checkbox" name="correct[0]" id="correct" value="0" />
<input type="checkbox" name="correct[1]" id="correct" value="1" />
<input type="checkbox" name="correct[2]" id="correct" value="2" />
<input type="checkbox" name="correct[3]" id="correct" value="3" />
<input type="checkbox" name="correct[4]" id="correct" value="4" />
<input type="checkbox" name="correct[5]" id="correct" value="5" />
If someone could point out what jQuery to use to make these checkboxes selected, that would be great!
<?php foreach($toCheck as $checkMe) { ?>
//i'm assuming my jquery goes here but I can't get it working
<?php }; ?>
Thank you in advance :)
Your HTML is invalid. id values must be unique within a document (reference).
That said, ignoring the id values, you could use this (live example):
$(':checkbox[name^=correct]').each(function() {
switch (this.value) {
case "0":
case "3":
case "4":
this.checked = true;
break;
}
});
That uses a CSS3 substring selector (jQuery supports nearly all of CSS3) on the name attribute to select any checkbox with a name starting with correct, and then uses jQuery's each to loop through them. Then we set the checked property on the ones with the desired value.
You could also do it with a longer selector and without the loop (live example):
$(':checkbox[name^=correct][value=0], :checkbox[name^=correct][value=3], :checkbox[name^=correct][value=4]').attr('checked', true);
Update: Re-reading your question, it looks like you might need to do this more dynamically:
<script type='text/javascript'>
(function() {
var boxes = $(); // Assumes jQuery 1.4 or higher
<?php foreach($toCheck as $checkMe) { ?>
echo "boxes.add(':checkbox[name=^=correct][value=" . $toCheck . "]');";
<?php }; ?>
boxes.attr('checked', true);
})();
</script>
...which would generate:
<script type='text/javascript'>
(function() {
var boxes = $(); // Assumes jQuery 1.4 or higher
boxes.add(':checkbox[name=^=correct][value=0]');
boxes.add(':checkbox[name=^=correct][value=3]');
boxes.add(':checkbox[name=^=correct][value=4]');
boxes.attr('checked', true);
})();
</script>
...which would check the relevant boxes. But it wouldn't be very efficient (all of that document traversal), better off using PHP to combine the values into a selector or switch as shown above.
Be aware that browsers don't submit unchecked checkboxes.
See Submit an HTML form with empty checkboxes for more.
i have an html form full of text fields, checkbox's , and radio fields.
i wanted to get all the names of the fields so that i can get started in validating the information in them.
the method i am using to get them is
if(isset($_POST['submit'])) {
foreach($_POST as $name => $value) {
print $name."<br/>";
}
}
but i noticed that it only displays textbox and textarea field names and it doesnt include checkbox and radio field names through this submission. do i need to include anything for it to grab the field names of those?
Checkboxes and radio buttons work a little differently than your standard inputs. If a checkbox is present on a form that doesn't necessarily mean that it will be available in the resulting POST information. Rather, those values will only be avialable if they are actually marked (checkboxes checked and radio buttons selected). The proper way to test for their value in PHP is not to check the field value but rather to check isset() first.
For a checkbox:
$data['my_checkbox'] = isset($_POST['my_checkbox']) ? 'on' : 'off';
and for a radio button:
$data['my_radio'] = isset($_POST['my_radio']) ? $_POST['my_radio'] : false;
To be a little more descriptive let's say you have the following form:
<form action="test.php" method="post">
<input type="text" name="email" value="" />
<input type="checkbox" name="active" value="Yes" />
<input type="submit" value="Submit" />
</form>
If I were to submit that form with an email value of 'test#email.com' but not check the checkbox I would have the following in $_POST:
Array (
'email' => 'test#email.com'
)
However, if I were to submit the same form with the same email address and check the checkbox I would have the following:
Array (
'email' => 'test#email.com',
'active' => 'Yes'
)
Hope that helps.
0./ Try using the following code to see the raw posted data:
echo '<pre>';
print_r($_POST);
echo '</pre>';
1./ Make sure you use a name attribute value for your checkbox and radio inputs.
Typically for checkboxes, it will be an array.
<input type="checkbox" id"=fruit-apple" name="fruits[]" value="apple" />
<input type="checkbox" id="fruit-pear" name="fruits[]" value="pears" />
2./ Make sure they sit inside the form tag.
3./ If you submit using a javascript call, try disabling javascript and see if the error stays. If it does not, you know your javascript is the culprit.