If checkbox with label is checked, submit form - php

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 . ' ';
}

Related

compare checkbox value on submit and display message using php

I want to display the message when user clicks on Others checkbox. There are other checkboxes too so I want to compare checkbox value and if Others checkbox is selected then I want to display the message. This is my code:
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2">Subject-1+Subject-2
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2+Subject-3">Subject-1+Subject-2+Subject-3
<input type="checkbox" name="subject[]" value="Others">Others
<input type="submit" value="Submit" name="submit" class="wpcf7-submit"><br><br>
php part:
if(isset($_POST['submit']))
{
if(!empty($_POST['subject']) && $_POST['subject']=="Others") {
echo " <SCRIPT LANGUAGE='Javascript'>
window.alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');
</SCRIPT>";
}
}
This code is not working. What changes should I make??
As subject[] is an array in
<input type="checkbox" name="subject[]" value="Others">
is an array type. So you should validate subject field as array as follows:
if (isset($_POST['submit'])) {
if (!empty($_POST['subject']) && in_array("Others", $_POST['subject'])) {
echo " <SCRIPT LANGUAGE='Javascript'>
window.alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');
</SCRIPT>";
}
}
You can try something like given below. you are free to use in_array() but if you want to use the second condition with it use foreach like this.
if (in_array("Others", $_POST['subject'])) {
echo 'selected ';
}
else{
//do something
}
OR
if(isset($_POST['submit']))
{
$results=$_POST['subject'];
foreach ($results as $result) {
if ($result==='Others') {
echo 'selected ';
}
else{
//do something
}
}
}
<?php
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
if(isset($_POST['subject'])){
$subject = $_POST['subject'];
if(in_array('Others', $subject)){
echo "<script>alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');</script>";
}
}else{
echo "<script>alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');</script>";
}
}
?>
<form method="POST" action="">
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2">Subject-1+Subject-2
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2+Subject-3">Subject-1+Subject-2+Subject-3
<input type="checkbox" name="subject[]" value="Others">Others
<input type="submit" value="Submit" name="submit" class="wpcf7-submit"><br><br>
</form>

Keeping checkbox state after submit with status checked on first page load

I have this html form with checkboxes that keep their status checked or unchecked after submitting the form and reloading the page:
<form method="post" action="">
<input type="checkbox" name="keyword1" value="keyword1" <?php if(isset($_POST['keyword1'])) echo "checked='checked'"; ?> />keyword1
<input type="checkbox" name="keyword2" value="keyword2" <?php if(isset($_POST['keyword2'])) echo "checked='checked'"; ?> />keyword2
<input type="checkbox" name="keyword2" value="keyword3" <?php if(isset($_POST['keyword2'])) echo "checked='checked'"; ?> />keyword3
<input type="submit" />
</form>
Problem is, that at first page load the checkboxes are unchecked. Is there any possibility to have all checkboxes with status checked at the beginning and then keep their new status after submit? So far I could not figure out how to do this. Any help would be much appreciated. Thanks,
you could use $_SESSION instead of $_POST :
<input type="checkbox" name="keyword1" value="keyword1" <?php if(isset($_SESSION['keyword1'])) echo "checked='checked'"; ?> />keyword1
And then put this on the top of your file :
session_start();
if (isset($_POST['my_form'])) {
if (isset($_POST['keyword1'])) {
$_SESSION['keyword1'] = 'checked';
}
}

2 forms with one PHP file

I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}

Is there a way read all checkboxes in forms and have one Submit button in a separate form?

This question will show what a newbie I am. The situation is this. It's a photo contest.
People upload a photo and to the right on the same line is a checkbox.
Voters check the box if they like the photo. They can select up to, say, 5 photos.
Keeping it simple, my problem isn't with MySQL, but with the form. Each row has a checkbox, which is a form. The SUBMIT button is the problem. The only way I can figure out to have submit work is by putting a submit button with each checkbox. Of course, that's ridiculous. What I want is to read all the checked boxes and have ONE submit button when the voter is finished. Spent hours on this and can't see how to have the SUBMIT by itself that the voter can click and have all the checked values inserted into the database a one time.
Any notions? I know this sound very primitive, but just getting into this.
Thanks ahead of time for any help
You can enclose all the checkboxes in a single html form tag and have the submit button inside it. It'll automatically submit data in all checkboxes
Or
You can use javascript/jQuery to run through all the checkboxes. And submit the form.
Well, there are all sorts of javascript ways to go out and grab the data you want and submit it, but let's keep it simple.
[voteform.html]
<form action="process_script.php">
<img src="image1"><input type="checkbox" name="vote_for_image[]" value="1">
<img src="image2"><input type="checkbox" name="vote_for_image[]" value="2">
<input type="submit">
</form>
... then ... (please note the [RETURN ERROR...] and [DATABASE...] blocks are just place holders for you to fill in code)
[process_script.php]
<?
if (count($_GET['vote_for_image'] > 5) {
[RETURN ERROR 'Please select no more than 5 images to vote for']
}
else {
foreach ($_GET['vote_for_image'] as $index => $image_id) {
[DATABASE INSERT OR UPDATE FOR $image_id]
}
}
?>
Set the checkbox[] as name attribute for each checkbox and on submission you can address the valiues as arrays
<body>
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox[]" value="a">
<input type="checkbox" name="checkbox[]" value="b">
<input type="checkbox" name="checkbox[]" value="c">
<input type="checkbox" name="checkbox[]" value="d">
<input type="submit" name="Submit" value="Submit">
</form>
<?php
if(isset($_POST['Submit']))
{
echo $_POST['checkbox'];
}
?>
</body>
the above will give you
Array (
[0] => a
[1]=>b
[2] => c
[3]=>d
)
Try this code
//HTML
<form method='post'>
<input type='checkbox' name='photo[]' value='1' />
<img src='test1.jpg' />
<input type='checkbox' name='photo[]' value='2'/>
<img src='test2.jpg' />
<input type='submit' value="Submit" name="submit" />
</form>
//PHP
if(isset($_POST['submit']))
{
if(isset($_POST['photo']))
{
if(count($_POST['photo']) > 5)
{
// Display error msg
}
else
{
// Contains all ids voted on
$img_ids=explode(',',$_POST['photo']);
// insert or update DB for the image ids
}
}
}

check box value in check form, php

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)

Categories