I have few checkboxes, I want to find whether all the checkboxes are checked and if yes return a message.
<label class="control-label col-md-3">L4 Deliverables</label>
<?php
while($subd_row=$subd_result->fetch_assoc()){
if($sub_row['selected'] == 1)
{
?>
<input class="flat" type="checkbox" name="L4d[]" value="<?php echo $subd_row['d_name'];?>" checked><?php echo $subd_row['d_name'];?></input>
}
Using the above code the checkboxes are displayed. The message could be for example: " 14 checkboxes are checked".
You can use $i to increment when it goes in that if statement it will increment,
<label class="control-label col-md-3">L4 Deliverables</label>
<?php $i = 0;
while ($subd_row = $subd_result->fetch_assoc()) {
if ($sub_row['selected'] == 1) {
$i++;
?>
<input class="flat" type="checkbox" name="L4d[]" value="<?php echo $subd_row['d_name']; ?>" checked><?php echo $subd_row['d_name']; ?></input>
<?php
}
}
?>
<label><?php echo ($i <= 1 ? "$i checkbox is ": "$i checkboxes are ")."checked"; ?></label>
On the backend, you can always check the length of the variables/array passed by a form.
L4d[] will have the values of checked checkboxes only.
You can simply check as:
if(count($_POST['L4d']))== 14) {...}
If you want something like an alert box to be popped up when all the checkboxes are checked, then you may call a javascript function 'onChange' of your checkbox field
Related
I'm writing a system and have a checkbox in a form.
My problem is, when i press the edit button, the checkbox cannot show me the tick in the checkbox, I have tried to change checkbox type to text, it can show me the value. I have set the value if value = 1 is tick, if value = 0 is no tick. How can shows up tick in the form? Anyone can guide me to solve it?
Below is my coding:
Checkbox
<div class="form-group col-lg-6">
<label class="control-label col-lg-4">Pricing<span style="color:red;"> </span></label>
<div class="col-lg-8">
<input type="text" name="rm_option" id="rm_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?> ><strong> RM </strong></input>
<input type="text" name="point_option" id="point_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?>><strong> Full Point </strong></input>
<input type="text" name="partial_option" id="partial_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?>><strong> Partial Point + RM </strong></input>
</div>
</div>
Checkbox function
<?php
$sql = "select * from promotion_list where id=" . $_GET['id'];
$arr_sql = db_conn_select($sql);
foreach ($arr_sql as $rs_sql) {
foreach ($rs_sql as $key => $value) {
?>
$("#<?php echo $key ?>").val("<?php echo $value?>");
<?php
}
?>
When I press the edit button, other column can show up in the form, only the checkbox cannot show me the tick. Below is the output picture:
Output
If I change Checkbox type to text, below is the output (Prove inside the checkbox got value)
Output 2
I have stuck in this problem already 1 week, hope someone coding hero can guide me to solve this problem. Thanks a lot.
Add below code exactly after this line:
$("#<?php echo $key ?>").val("<?php echo $value?>");
it became:
$("#<?php echo $key ?>").val("<?php echo $value?>");
<?php if($value == 1){ ?>
$("#<?php echo $key ?>").attr("checked", true).prop("checked", true);
<?php } ?>
its clear that you are using jquery to work on the checkbox.
I have a form of checkboxes as shown below:
<form method="POST" action="display.php">
<input type="checkbox" value="1" name="options[]">
<span class="checkboxText"> Fruits</span>
<input type="checkbox" value="2" name="options[]">
<span class="checkboxText">Vegetables </span><br><br>
<button class="button" type="submit" value="display">DISPLAY</button>
</form>
I get the options[] using $_POST['options'] and save the array of data in a variable. I want to display the array of fruits if the fruits checkbox is checked, the vegetables array if vegetables checkbox is checked and display both of them if both are checked and display a message saying "Fruits and Vegetables are healthy". This is the php code I have so far but it does not seem to work as I would like it to.
<?php
$values = $_POST['options'];
$n = count($values);
for($i=0; $i < $n; $i++ )
{
if($values[$i] === "1" && $values[$i] == "2")
{
//iteration to display both tables
echo 'Fruits and Vegetables are healthy';
}
else if($values[$i] === "1")
{
//display fruits
}
else if( $values[$i] == "2")
{
//display vegetables
}
}
?>
The problem with my php code is that is does not go into the first if at all. It just displays both tables from the other two ifs (since the echo is not displayed either). Is there any way I could solve this?
You shouldn't need a loop for this. You just need to check in $_POST['options'] for each of the values in question. I would suggest using the text you want to display as the values for your checkboxes so you don't have to convert from numbers to words.
<input type="checkbox" value="Fruits" name="options[]">
<span class="checkboxText"> Fruits</span>
<input type="checkbox" value="Vegetables" name="options[]">
<span class="checkboxText">Vegetables </span><br><br>
Then for the display, just output the fruits/vegetables arrays depending on whether or not those values are present in $_POST['options'].
if (!empty($_POST['options'])) {
echo implode(' and ', $_POST['options']) . " are healthy";
if (in_array('Fruits', $_POST['options'])) {
// show the fruits
}
if (in_array('Vegetables', $_POST['options'])) {
// show the veg
}
}
To save me making masses of edits to the rest of my code I want to make a checkbox act like a textbox.
I have the following textbox, that if a number is greater than 0 then it adds items to a basket.
<input type="textbox"
value="<? echo $item[qty]; ?>"
name="<? echo $productid."_".$product_quantity[id]."_".$product_option[id]; ?>"
/>
How can I make a checkbox act the same way? I.e If it's ticked it's value equals 1, therefore adding it to my basket. If it remains unticked then it's value equals 0, therefore ignored.
<input type="checkbox"
value="<? echo $item[qty]; ?>"
name="<? echo $productid."_".$product_quantity[id]."_".$product_option[id]; ?>"
/>
I have several of these checkboxes going down the page.
Change the value from value="<? echo $item[qty]; ?>" to value="1"
In the action of you form i.e. wishlist.php :
if(isset($_POST["name_of_checkbox"])) {
//It means check box has been ticked
$myvalue = 1;
} else {
//It means check box was not ticked
$myvalue = 0;
}
HTH
This is another approach to the question maybe this is what you need.
I would introduce a hidden field which will store/post the 0 or 1 value. We will bind an event with the checkbox that will update the value of the hidden input.
<input name="my_cb" id="my_cb" type="checkbox" value="1" />
<input type="hidden" name="my_cb_hidden" id="my_cb_hidden" value="" />
<script type="text/javascript">
$("#my_cb").click(function() {
if($("#my_cb").is(":checked")){
$("#my_cb_hidden").val($(this).val());
} else {
$("#my_cb_hidden").val(0);
}
alert($("#my_cb_hidden").val());
});
you can change the value of checkbox value from
value="<? echo $item[qty]; ?>"
to
value="1"
then if it is checked then on Submit it will automatically store 1 as a value
EDITED :
after submitting in php code do it as as you updated your question
if(isset($_POST['checkbox name']))
{
$item[qty] = 1;
}
else
{
$item[qty] = 0;
}
but this will only make the changes in the $item array's value inspite of the value your checkbox hold.
I have a field in my update form called approve which is using the html checkbox element. now i am querying the approve value from the database which will hold the binary value (0 or 1), i want the checkbox to perofrm the following actions in condition.
a) While Querying from database.
1)if the value of active is 1 then it should be checked by default and also it should hold the value 1 to process it to update
2)the same applies for 0, if the value is zero then it is unchecked and will hold the value 0 to process
P.S: I want to use this for updating the form not inserting.
Do it like this:
PHP embedded in HTML way:
<input name="chk" type="checkbox" value="<?=$value?>" <?php if($value==1) echo 'checked="checked"';?> />
Pure PHP way:
<?php
echo '<input name="chk" type="checkbox" value="'.$value.'"';
if($value == 1)
echo ' checked="checked" ';
echo '/>';
?>
Just a 1-line shorter version ;)
<?php echo "<input name=\"chk\" type=\"checkbox\" value=\"$value\"".( ($value == 1) ? " checked=\"checked\"" : "" )." />"; ?>
Like this:
<input type="checkbox" value="<?php echo $row['approved']; ?>" <?php if($row['approved'] == 1): echo 'checked="checked"'; endif; />
For usability purposes I like to set up my form fields this way:
<?php
$username = $_POST['username'];
$message = $_POST['message'];
?>
<input type="text" name="username" value="<?php echo $username; ?>" />
<textarea name="message"><?php echo $message; ?></textarea>
This way if the user fails validation, the form input he entered previously will still be there and there would be no need to start from scratch.
My problem is I can't seem to keep check boxes selected with the option that the user had chosen before (when the page refreshes after validation fails). How to do this?
My first suggestion would be to use some client-side validation first. Maybe an AJAX call that performs the validation checks before continuing.
If that is not an option, then try this:
<input type="checkbox" name="subscribe" <?php echo (isset($_POST['subscribe'])?'checked="checked"':'') ?> />
So if subscribe is = 1, then it should select the box for you.
For Example, consider the following code for checkbox :-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
Then, this would remember the checkbox of "PHP" if it is checked, even if the validation for the page fails and so on for "n" number of checkboxes as shown below:-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
HTML<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("HTML", $_POST["course"]))) {
echo "checked";
} ?> value="HTML" />
CSS<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("CSS", $_POST["course"]))) {
echo "checked";
} ?> value="CSS" />
Javascript<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("Javascript", $_POST["course"]))) {
echo "checked";
} ?> value="Javascript" />
And most importantly, do not forget to declare the "course" variable as an array at the start of the code as shown below :-
$course = array();
I have been battling how to create sticky check box (that is able to remember checked items any time you visit the page). Originally, I get my values from a database table. This means that my check box value is entered to a column on my db table.
I created the following code and it works just fine. I did not want to go through that whole css and deep coding, so...
CODE IN PHP
$arrival = ""; //focus here.. down
if($row['new_arrival']==1) /*new_arrival is the name of a column on my table that keeps the value of check box*/
{$arrival="checked";}// $arrival is a variable
else
{$arrival="";};
echo $arrival;
<b><label for ="checkbox">New Arrival</label></b>
<input type="checkbox" name ="$new_arrival" value="on" '.$arrival.' /> (Tick box if product is new) <BR><BR>
<input type="checkbox" name="somevar" value="1" <?php echo $somevar ? 'checked="checked"' : ''; ?>/>
Also, please consider sanitising your inputs, so instead of:
$somevar = $_POST['somevar'];
...it is better to use:
$somevar = htmlspecialchars($_POST['somevar']);
When the browser submits a form with a checked checkbox, it sends a variable with the name from the name attribute and a value from the value attribute. If the checkbox is not checked, the browser submits nothing for the checkbox. On the server side, you can handle this situation with array_key_exists(). For example:
<?php
$checkedText = array_key_exists('myCheckbox', $_POST) ? ' checked="checked"' : '';
?>
<input type="checkbox" name="myCheckbox" value="1"<?php echo $checkedText; ?> />
Using array_key_exist() avoids a potential array index undefined warning that would be issued if one tried to access $_POST['myCheckbox'] and it didn't exist.
You may add this to your form:
<input type="checkbox" name="mycheckbox" <?php echo isset($_POST['mycheckbox']) ? "checked='checked'" : "" ?> />
isset checks if a variable is set and is not null. So in this code, checked will be added to your checkbox only if the corresponding $_POST variable has a value..
My array has name="radioselection" and value="1", value="2", and value="3" respectively and is a radio button array... how to I check if the radio value is selected using this code
I tried:
<?php echo (isset($_POST['radioselection']) == '1'?'checked="checked"':'') ?> />