CheckBox Value to Text in DB - php

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.

Related

How to post a button's value in html form (PHP)

i have javascript that changes an input's value and when i post the form, the field is not transferred
<input id="exampleID" type="button" name="exampleButton" value="click me" onclick="changeValue()">
when the button is clicked the value (displayed to the user) changes to "Clicked"
then when i post the form it the value is left behind
if(isset($_POST['exampleButton'])) {
echo "true";
}
this doesn't echo anything
what should i do?
You can change value of hidden field:
<input id="exampleID" type="hidden" name="exampleButton" value="">
<input type="button" value="click me" onclick="changeValue()">
<script>
function changeValue(){
document.getElementById('exampleID').value = "Clicked";
}
</script>
The input button control won't send any value to the server on submit, as it is made to interact with javascript. If you need to send a dynamic value, store it in a field (like an input hidden control), or, if you insist to send it through a button, change its type to submit

form submit checkbox value

I have a bike I have a car
I know when the form is submitted the value will be vehicle=Bike&vehicle=Car if both are ticked
Is there a way to make the value to be vehicle=Bike,Car
Put them into one variable then separated in a comma
Since you are using POST and multiple check boxes, set the name of each check box like this:
<input type="checkbox" name="vehicle[]" value="Bike">
<input type="checkbox" name="vehicle[]" value="Car">
Then when your form is submitted you will receive an array of all the checked boxes and their values in the array:
$_POST['vehicle'][];
Now if both boxes are checked you can retrieve the values in a foreach loop:
foreach($_POST['vehicle'] as $type){
echo "Type = ".$type;
}
With this you will get an output of
Type = Bike
Type = Car
Try this one,
<input type="hidden" name="vechiclesStr" id="vechiclesStr">
//on submitting,
document.getElementById("vechiclesStr").value = document.formname.vechicle.join();

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

get value attribute of a checkbox from javascript function

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

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