i have been looking for this script for a while now. I have some rules and then i have a checkbox to click if you agree the terms and rules.
Now how do i make a check in PHP if the person has checked that box and agreed to the rules? Thanks for ur help, i appreciate it!
Assuming you have a form that looks something like this:
<form method="post" action="some_handler.php">
<label for="option1">Option 1</label>
<input id="option1" type="checkbox" name="option1" />
<label for="option2">Option 2</label>
<input id="option2" type="checkbox" name="option2" />
<!-- submit, etc -->
</form>
You can check for the presence of the checkbox values (by name) in $_POST, i.e.
<?php
$optionOne = isset( $_POST['option1'] );
$optionTwo = isset( $_POST['option2'] );
If the boxes aren't checked, $_POST won't contain values for them.
It's totally enough to check for:
$userAgrees = false;
if (isset($_POST['myCheckbox']))
{
$userAgrees = true;
}
If the form is a method POST form. Then on the action page you should have access to the $_POST variable.
Check out the results of this on your action page.
echo "<pre>";
print_r($_POST);
echo "</pre>";
The $_POST variable will be an array. You can access the value of the array like this.
if($_POST["key"] == "value")
Where the key is the name in the output above.
form.php:
<form action="checkbox-form.php" method="post">
<label for="formWheelchair">Do you need wheelchair access?</label>
<input type="checkbox" name="formWheelchair" value="Yes" id="formWheelchair" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
checkbox-form.php:
if(isset($_POST['formWheelchair']) &&
$_POST['formWheelchair'] == 'Yes')
{
$wheelchair = true;
}
else
{
$wheelchair = false;
}
var_dump( $wheelchair );
// shorthand version:
$wheelchair = isset($_POST['formWheelchair'])?true:false;
Straight from: http://www.html-form-guide.com/php-form/php-form-checkbox.html
Note: At a later point you may want to use sessions to store the data for server-side validation if the user has not entered in all the fields.
In form html:
<input type="checkbox" name="terms">
In php script that the form posts to:
if ( $_POST['terms'] == 'on' ) {
echo 'User accepted terms';
}
Related
I have this HTML code:
<form action="index.php" method="post">
<input id="firstCheck" type="checkbox" name="test[]" value="first" checked>
<label for="firstCheck">1</label><br>
<input id="secondCheck" type="checkbox" name="test[]" value="second" checked>
<label for="secondCheck">2</label><br>
<input type="submit" name="test_submit" value="Send">
</form>
Also I have this PHP code:
function create_cookies()
{
if (isset($_POST['test_submit'])) {
$checkboxes_array = $_POST['test'];
setcookie("testcookie1234556", json_encode($checkboxes_array), time()+3600);
}
}
create_cookies();
function check_cookies()
{
if (isset($_COOKIE['testcookie1234556'])) {
$my_cookie_array = json_decode($_COOKIE['testcookie1234556']);
return $my_cookie_array;
} else {
return false;
}
}
check_cookies();
As you can see, I have got all checked checkboxes and stored it to the php cookie.
Now, when someone is logging to the system, all checkboxes is checked by default. But I need them to be checked or unchecked based on the info from php cookie!
I mean, for example, if user logged on to the system and unchecked some checkboxes, logged off, then logged in again, previously unchecked checkboxes must remains unchecked.
Please, give me some advices for doing it.
Put your cookie in an array say
$cookie_aray=check_cookies();
and check it every time you load a check box, whether it was selected or not.
<html><form action="abc.php" method="post">
<input id="firstCheck" type="checkbox" name="test[]" value="first" <?php if(in_array('first',$cookie_aray)) echo 'checked';?>>
<label for="firstCheck">1</label><br>
<input id="secondCheck" type="checkbox" name="test[]" value="second" <?php if(in_array('first',$cookie_aray)) echo 'checked';?>>
<label for="secondCheck">2</label><br>
<input type="submit" name="test_submit" value="Send">
I have multiple checkboxes with names of adminMeta[], such as:
<input type="checkbox" name="adminMeta[name1]" value="1" />
<input type="checkbox" name="adminMeta[name2]" value="1" />
and so on and I also have text inputs like this too with the same names.
When the data is posted, I am looping through using a foreach loop:
foreach($_POST["adminMeta"] as $a => $b) {
}
inside the loop, I add/update the record in my database depending on whether it exists already or not.
But I am having some issues with checkboxes and knowing whether they are checked or not.
I have tried using if(isset($b)) but that hasn't worked.
How can I tell inside my loop, whether a checkbox is checked or not?
If a checkbox is not checked, then it is not a successful control.
If it is not a successful control, then it won't be included in the form data at all.
If it isn't in the form data, then it won't appear when you loop over the form data.
So
If it is in the form data, then it is checked
Otherwise it is not checked
Normally I'd approach this problem with something along the lines of:
$list_of_checkboxes = [ "name1", "name2" ];
Then generate the form with:
foreach ($list_of_checkboxes as $name) {
?>
<label>
<input type="checkbox"
name="adminMeta[]"
value="<?php echo htmlspecialchars($name); ?>">
<?php echo htmlspecialchars($name); ?>
</label>
<?php
}
Then test the data with:
foreach ($list_of_checkboxes as $name) {
if (in_array($name, $_POST['adminMeta'])) {
# Checked
} else {
# Not checked
}
}
Another approach would be to set hidden inputs before each check with default value of 0:
<input type="hidden" name="adminMeta[name1]" value="0" />
<input type="checkbox" name="adminMeta[name1]" value="1" />
<input type="hidden" name="adminMeta[name2]" value="0" />
<input type="checkbox" name="adminMeta[name2]" value="1" />
Now you will receive the data even if you don't check the checkboxes.
I cant seem to get my php script to recognize that the checkbox in my form is checked.
I want to accomplish this:
If checkbox is checked, the php script should submit to my DB.
PHP:
<?php
if (!empty($_POST['approve_student'])) {
if (isset($_POST['approve'])) {
//submit
} else {
//do nothing
}
}
?>
FORM:
<input class="checkbox" name="approve" type="checkbox" id="approve">
<label name="approve" for="approve"><span><div data-textbox="1" ></div></span></label>
<input class='button_submit_2' name="approve_student" type="submit" value='Submit'>
NOTES:
Im using Jquery to keep checkbox checked during session.
Im using CSS to customize checkbox.
I dont know if these two might corrupt anything.
Hope u can help.
Your code is fine it is working
if (!empty($_POST['approve_student'])) {
if (isset($_POST['approve'])) {
echo "Approved";
} else {
echo "Not Approved";
}
}
<form action="index.php" method="post">
<input class="checkbox" name="approve" type="checkbox" id="approve">
<label name="approve" for="approve"><span><div data-textbox="1" ></div></span></label>
<input class='button_submit_2' name="approve_student" type="submit" value='Submit'>
</form>
If the checkbox isn't checked then the value shouldn't be coming through in the first place, so this should work;
if(isset($_POST["testvariabel"])){
//whatever you wanna do here
}
or if it's always coming through...
Change the markup to something like this, setting a value property
<input type="checkbox" class='form' onclick="this.value=!this.value" value=true name="checkbox[]" />
and to get the values submitted, use a simple loop
if($_POST['checkbox'] == 0){
echo $checkbox . ' ';
}
I am trying to save fields data after submited, Becouse after all the fields are good to go but lets say at the server side the user name is already taken so the form return empty and i dont want that there is the option to do it with PHP like that:
<input value="<?php if(isset($userName)) echo $userName; ?>" />
But the problem is with the radio input, If can some one think about solution about the radio with PHP i will be very thankful, Also i was thinking about Javascript so i will have cleaned code and i was thinking about taking the values from the URL but i am using POST for security reasons.
Summary: If anyone have a solution with PHP or Javascript i will be very thankful, Thank you all and have a nice day.
Try this
<form name="myform" action="" method="post">
<input type="radio" name="language" value="Java" <?php echo(#$_POST['language'] == 'Java'?"checked":""); ?> /> Java
<input type="radio" name="language" value="VB.Net" <?php echo(#$_POST['language'] == 'VB.Net'?"checked":""); ?> /> VB.Net
<input type="radio" name="language" value="PHP" <?php echo(#$_POST['language'] == 'PHP'?"checked":""); ?> /> PHP
<input type="submit" />
I think this may help you.
<input type="radio" value="choice1" name="radio_name" <?php echo(#$_POST['radio_name'] == 'on'?"checked":""); ?> />
If you want to automatically select a radio input you can add the attribute checked to it. What you are going to need will look like this :
<form method="POST">
<?php
// You have some short of list of possible value //
$arrRadioValues = array("value1", "value2", "value3");
// You display them //
for ($i=0; $i<count($arrRadioValues); $i++) {
?>
<input
type="radio"
name="radioInputName"
value="<?php echo $arrRadioValues[$i]; ?>"
<!-- If the value that was posted is the current one we have to add the "checked" so that it gets selected -->
<?php if (isset($_POST['radioInputName']) && $_POST['radioInputName'] == $arrRadioValues[$i]) { echo " checked"; } ?> />
<?php
}
?>
<input type="submit" />
</form>
Adding the checked attribute works a little bit in the same as setting a value to an input. It's just that instead of defining the value attributes, you define the checked attribute when you want that radio to be selected.
I am modifying a php login form, adding javascript check form function to it. I wish when users tick the checkbox, the form is true, and when the checkbox is empty, the form becomes false. the codes are like these:-
//the javascript
function check_sli(form,mark,edit){
if(mark==1 || mark=="all"){
if(form.terms.value==""){
sli_check_terms.innerHTML="Please read the terms and conditions first!";
sli_check_terms.style.height="auto";
return false;
}else{
sli_check_terms.innerHTML="";
sli_check_terms.style.display = "none";
}
}
}
//the form
<form name="form_sli" id = "form_sli" ACTION="<?php echo $loginFormAction1; ?>" METHOD="POST" onSubmit="return check_sli(form_sli,'all')">
<input type="text" name="login"/><br>
<input type="password" name="password"/><br>
<input name="terms" type="checkbox" id="terms" checked="checked" onBlur="check_sli(form_sli,1)">I have read the terms and conditions<br><div id="sli_check_terms" class="right"></div>
<input type="submit" name="button" id="button" value="Login" />
</form>
The above codes works normal for textfields, such as when the textfield is empty, the innerHTML pops up. However, when using checkbox, I don't know the checked and unchecked value, is it 1 vs 0, or !=="" vs ==""??? and shall I use onBlur or onSubmit???
How shall it modify the scripts so that it works for checkbox as well? thanksalot!
checkboxes use .checked in the dom, which is a bool.
if (form.terms.checked) {
... it's checked ...
}
Instead of if(form.terms.value==""), do this if(form.terms.checked==false)