not getting values from my post - php

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+

Related

Codeigniter Getting the value of a checkbox

I want to get the value of a checkbox once it checked.
From my code
$vat_checkbox = $this->input->post('vat_checkbox');
print_r($vat_checkbox);
die();
it display "on". I will use it for validation. What would be the proper way without using javascript?
When checkbox is checked you will get value as on else you will not get that tag also. so You can do something like this.
$post['vat_checkbox'] = $this->input->post('vat_checkbox');
if(isset($post['vat_checkbox']) && $post['vat_checkbox'] == 'on') {
$post['vat_checkbox'] = 1;
} else {
$post['vat_checkbox'] = 0;
}
Here $post['vat_checkbox'] you can use for operation
Just add value attribute in input tag if you dont want to do with jquery
<form action="test.php" method="POST">
<input type="checkbox" name="vat_checkbox_1" value="1" checked>
<input type="checkbox" name="vat_checkbox_2" checked>
<input type="submit" name="submit" value="SUBMIT">
</form>
If you added value then it will give you value from value attribute else "on"
Output:
Array
(
[vat_checkbox_1] => 1
[vat_checkbox_2] => on
[submit] => SUBMIT
)
Here, In Controller you can directly use your checkbox data without any manipulation.

How to remember checkbox state when default state is checked?

I have a form with a check box.
The check box has three "states":
Checked as default.
Unchecked by user.
Checked again by user.
How would I do something like that? If I use !isset(), I can uncheck the checkbox but it doesn't distinguish if it was unchecked by the user or if it's suppose to be checked as default.
I want to use this to refill the last values of the form inputs whenever there are form errors on submit.
It's easy to do when the default state of the check box is unchecked. I can then just do:
if (isset($_POST['checkbox'])) echo ' checked'; //check box has been checked
The cleanest way of maintaining the form's state after submission is to use an array to store the input values. First you set the default values, then if the form has been submitted, overwrite the defaults with whatever was submitted. In the HTML form, always output the value from the form array.
// create the array (for both viewing the form and maintaining the submitted values)
$form = array(
'myCheckbox' => true // default to checked
'firstName' => '',
)
// was the form submitted?
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// overwrite the form array
$form['myCheckbox'] = isset($_POST['myCheckbox']);
$form['firstName'] = $_POST['firstName'];
// at this point do any validation, if it fails, let the form show
}
Now for the form:
<form method="post">
<input type="checkbox" name="myCheckbox" value="1" <?php echo $form['myCheckbox'] ? 'checked' : '' ?> />
<input type="text" name="firstName" value="<?php echo htmlspecialchars($form['firstName']); ?>" />
</form>
In the above, I added a text input firstName to illustrate that all of the form inputs should be managed by the array.
you can use javascript with an input hidden to tell how many times the checkbox was clicked
<form action="">
<input id="bike" type="checkbox" name="vehicle" value="Bike" checked onchange="document.getElementById('bike-state').value=parseInt(document.getElementById('bike-state').value)+1">I have a bike
<input type="hidden" id="bike-state" name="count" value="0">
</form>
now in the server side if $_POST['bike-state'] is an odd then it was unchecked, if it is even and greater than 0 it was unchecked and then rechecked , otherwise if still 0 it was checked by default

Posting both checked and unchecked checkboxes

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
)

How to validate multi-checkbox state?

I am using position absolute's validation engine for my form. I would like to check whether at least one checkbox from group is selected. In examples it is done by setting the same name attribute for group of checkboxes.
I cannot name checkboxes with the same name, because I am saving their state in database with following code:
$values = array(
'checkbox1' => null,
'checkbox2' => null
);
foreach (array_intersect_key($_POST, $values) as $key => $value) {
$values[$key] = mysql_real_escape_string($value);
}
$query_add_candidate=sprintf("INSERT INTO dbase (checkbox1, checkbox2) VALUES ('$values[checkbox1]', '$dates[checkbox2]')"
Now checkbox1 and checkbox2 are validated individually, beacuse they have different names. How can I check if selected is at least one of them?
Here is my HTML code:
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="checkbox1" id="maxcheck1" value="1"/> Text1
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="checkbox2" id="maxcheck2" value="2"/> Text2
on php ,
if(!$_POST['checkbox1'] && !$_POST['checkbox2']){
echo 'Error check at least one';
}
but what you really want is an array,
HTML,
<input type="checkbox" value="ch1" name="check[]" />
<input type="checkbox" value="ch2" name="check[]" />
php
<?php
if(empty($_POST['check'])){
echo 'Error: hey, check at least one will you!?';
}
?>
so this way you don't have to check all of them one by one, especially if you have loads of them on the same page.
NOTICE: You should also know, if checkbox is not ticked it will also not be set on the php $_POST superglobal, otherwise if it is ticked, it will show whatever the value="..." holds,
if its posted then its checked,
so if you have it in $_POST["checkbox_name"] then its checked, otherwise it wont be posted.
You can either add loads of code to reimplement control arrays in a poor way, or you can alter the code that builds your query so it can accept control arrays.
I would prefer the latter.

displaying all form field names but checkbox's and radios fields dont show

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.

Categories