get value attribute of a checkbox from javascript function - php

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();" >

Related

Get value from checkbox - 0 when unchecked, 1 when checked

I need to get value 1 - when checkbox is checked and 0, when it is unchecked.
<input type="checkbox" name="data[color][]" class="color" value="1" checked>
Data array - stores other data that is retrieved from input type text and select. It's working fine. The problem occurs when I try to retrieve data from an input of type checkbox. Brief description of the "project": the script is to add several records to the database simultaneously. To start with, one section is available for data entry, for a single record - using JS after clicking the appropriate button - the next section appears and so on up to a certain maximum number of sections/possible records. In general, the script works correctly - records are added. The problem is with the color column that gets the data from the aforementioned checkbox. After the form is submitted - there is a reorganization of the board to somehow present this more reasonably.
PHP code:
foreach($data as $columnName => $columnValues){
foreach($columnValues as $rowIndex => $columnValue){
$result[$rowIndex][$columnName] = $columnValue;
}
}
To get at the value:
foreach($result as $r => $value){
if(isset($value['color'])){
$color = 1;
} else {
$color = 0;
}
}
While with the number of records <= 2 it works correctly, when there are > 2 values are not assigned correctly. What am I doing wrong? If you need more information - please write. I seem to have pasted everything related to this.
When using var_dump for $value['color'] with 4 records there is this result:
string(1) "1"
string(1) "1"
NULL
NULL
Where only the first and fourth checkbox was checked.
Unlike input type text and select, only checked checkbox inputs will be submitted. So using dynamic arrays [], if you have 3 and only the first is checked, then it will be submitted as $_POST['data']['color'][0]. If you only check the third one it will also be submitted as $_POST['data']['color'][0], etc... With the current code there is no way to track them.
Probably the best way to do this is to use a hidden input with the same name before the checkbox with value 0 as default, so that all checkboxes are submitted, either with 0 or 1. You need to specify the numerical indexes for each:
<input type="hidden" name="data[color][0]" value="0">
<input type="checkbox" name="data[color][0]" class="color" value="1" checked>
<input type="hidden" name="data[color][1]" value="0">
<input type="checkbox" name="data[color][1]" class="color" value="1" checked>
<input type="hidden" name="data[color][2]" value="0">
<input type="checkbox" name="data[color][2]" class="color" value="1" checked>
Now you'll always get 3 $_POST['data']['color'] elements, either 0 or 1.

CheckBox Value to Text in DB

I am making a project. How to convert CheckBox tick value to Yes for MySQL database and untick no?
I was able to make simple HTML inputs of Check Boxes. How to set the things above in PHP?
To get the result of the checkbox into a PHP script, submit it as a field in a form either through POST or GET, then in your targeted php script, process the $_POST or $_GET superglobal you chose:
if (isset($_POST['Check']) && !empty($_POST['Check'])) {
$Result = $_POST['Check'];
}
To get the result of whether the checkbox is checked or not into the field you can use a single checkbox, and a hidden field in the form to hold the value "Yes" or "no"
<fieldset>
<form action="check.php" method="post">
<input type="checkbox" checked="true" onclick="getSelected(this)" value="Yes">Query
<input type="hidden" name="Check" id="Status" value="Yes">
<button type="submit">Submit</button>
</form>
</fieldset>
To change the value of the hidden field when the checkbox is checked or unchecked use javascript to set the value of the field:
// Set Hidden Field to Result of Checkbox
function getSelected(elem) {
// Get Hidden Field Reference
var status = document.getElementById("Status");
// Determine if Checked
if(elem.checked) {
status.value = "Yes";
} else {
status.value = "No";
}
}
Then when you submit the form, it is POSTed to the target script and you will have a field "Check" that is either "Yes" or "No". From there you can use PHP to process your variables and then run whatever MySQL queries you need.
Hope this helps.

Get several checkboxes id's after submission

I have code similar to the following:
<input type="checkbox" name="visitProperty" value="1" id="visit-0">
<input type="checkbox" name="visitProperty" value="1" id="visit-1">
<input type="checkbox" name="visitProperty" value="1" id="visit-2">
...
Once the form is submitted I want to get checked checkboxes, so far I've been using
if (isset($_POST['visitProperty']) {..}
But to my understanding it only gets one checkbox? Where as I need to check all of them and see if they were checked, so inside the if statement I can create a loop that gets id's of all submitted checkboxes and then gets the number from id, to update a certain array.
<input type="checkbox" name="visitProperty[]" value="1" id="visit-0">
<input type="checkbox" name="visitProperty[]" value="2" id="visit-1">
<input type="checkbox" name="visitProperty[]" value="3" id="visit-2">
<?php
foreach($_POST['visitProperty'] as $check) {
echo $check . "<br>"; // for example
}
?>
NOTE: $_POST['visitProperty'] will hold checked checkbox values. You will access all the checkboxs as an array as following $_POST['visitProperty'][]
When you put in the name, you are declaring a variable. You need to declare it as an array, or each checkbox will bump out the last one. Add some empty square brackets to the name.
You would need or defined ID or a unique value, otherwise you will not be able to identify them on the server-side (the ID does not get sent in $_POST).
So in case of a unique, identifyable ID, you could do something like:
<input type="checkbox" name="visitProperty[<?php // echo some unique id from a database for example ?>]" value="1" id="visit-0">
The reason you would need the ID to be identifyable, is that unchecked checkboxes do not get sent to the server, so you might end up with an array of 2 if visitProperty is an array, but you would not know which 2.

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.

Submit an HTML form with empty checkboxes

I have an HTML form - with PHP, I am sending the data of the form into a MySQL database. Some of the answers to the questions on the form have checkboxes. Obviously, the user does not have to tick all checkboxes for one question. I also want to make the other questions (including radio groups) optional.
However, if I submit the form with empty boxes, radio-groups etc, I received a long list of 'Undefined index' error messages for each of them.
How can I get around this? Thanks.
I've used this technique from time to time:
<input type="hidden" name="the_checkbox" value="0" />
<input type="checkbox" name="the_checkbox" value="1" />
note: This gets interpreted differently in different server-side languages, so test and adjust if necessary. Thanks to SimonSimCity for the tip.
Unchecked radio or checkbox elements are not submitted as they are not considered as successful. So you have to check if they are sent using the isset or empty function.
if (isset($_POST['checkbox'])) {
// checkbox has been checked
}
An unchecked checkbox doesn't get sent in the POST data.
You should just check if it's empty:
if (empty($_POST['myCheckbox']))
....
else
....
In PHP empty() and isset() don't generate notices.
Here is a simple workaround using javascript:
before the form containing checkboxes is submitted, set the "off" ones to 0 and check them to make sure they submit. this works for checkbox arrays for example.
///// example //////
given a form with id="formId"
<form id="formId" onSubmit="return formSubmit('formId');" method="POST" action="yourAction.php">
<!-- your checkboxes here . for example: -->
<input type="checkbox" name="cb[]" value="1" >R
<input type="checkbox" name="cb[]" value="1" >G
<input type="checkbox" name="cb[]" value="1" >B
</form>
<?php
if($_POST['cb'][$i] == 0) {
// empty
} elseif ($_POST['cb'][$i] == 1) {
// checked
} else {
// ????
}
?>
<script>
function formSubmit(formId){
var theForm = document.getElementById(formId); // get the form
var cb = theForm.getElementsByTagName('input'); // get the inputs
for(var i=0;i<cb.length;i++){
if(cb[i].type=='checkbox' && !cb[i].checked) // if this is an unchecked checkbox
{
cb[i].value = 0; // set the value to "off"
cb[i].checked = true; // make sure it submits
}
}
return true;
}
</script>
To add to fmsf's code, when adding checkboxes I make them an array by having [] in the name
<FORM METHOD=POST ACTION="statistics.jsp?q=1&g=1">
<input type="radio" name="gerais_radio" value="primeiras">Primeiras Consultas por medico<br/>
<input type="radio" name="gerais_radio" value="salas">Consultas por Sala <br/>
<input type="radio" name="gerais_radio" value="assistencia">Pacientes por assistencia<br/>
<input type="checkbox" name="option[]" value="Option1">Option1<br/>
<input type="checkbox" name="option[]" value="Option2">Option2<br/>
<input type="checkbox" name="option[]" value="Option3">Option3<br/>
<input type="submit" value="Ver">
Use this
$myvalue = (isset($_POST['checkbox']) ? $_POST['checkbox'] : 0;
Or substituting whatever your no value is for the 0
We are trouble on detecting which one checked or not.
If you are populating form in a for loop, please use value property as a data holder:
<?php for($i=1;$i<6;$i++):?>
<input type="checkbox" name="active[]" value="<?php echo $i ?>"
<?endfor;?>
If submit form you'll get order numbers of checkboxes that checked (in this case I checked 3rd and 4th checkboxes):
array(1) {
["active"]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "4"
}
}
When you are processing form data in loop, let's say in post.php, use following code to detect if related row is selected:
if(in_array($_POST['active'] ,$i))
$answer_result = true;
else
$answer_result = false;
Final code for testing:
<?php if (isset($_POST) && !empty($_POST)):
echo '<pre>';
var_dump($_POST);
echo '</pre>';
endif;
?>
<form action="test.php" method="post">
<?php for($i=1;$i<6;$i++):?>
<input type="checkbox" name="active[]" value="<?php echo $i; ?>" />
<?php endfor;?>
<button type="submit">Submit</button>
</form>
Although many answers were submitted, I had to improvise for my own solution because I used the customized check-boxes. In other words, none of the answers worked for me.
What I wanted to get is an array of check-boxes, with on and off values. The trick was to submit for each check-box on/off value a separator. Lets say that the separator is ";" so the string you get is
;, on, ;, ;, ;
Then, once you get your post, simply split the data into array using the "," as a character for splitting, and then if the array element contains "on", the check-box is on, otherwise, it is off.
For each check-box, change the ID, everything else is the same... and syntax that repeats is:
<div>
<input type="hidden" name="onoffswitch" class="onoffswitch-checkbox" value=";" />
...some other custom code here...
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch1" checked>
</div>
EDIT: instead of the ";", you can use some KEY string value, and that way you will know that you did not mess up the order, once the POST is obtained on the server-side... that way you can easily create a Map, Hash, or whatever. PS: keep them both within the same div tag.

Categories