read only property for checkbox - php

Is there a read only property for a checkbox?
Because I can still tick on the checkbox even if I have this code, is the read only property only for text box? What's the code if you want the check box to be read-only?
<td><input name="stats1" type="checkbox" id="SSS" readonly="readonly" value="<?php echo $row["STAT"]; ?>" <?php echo $row["STAT"] ? 'checked="checked"' : ''; ?> >SSS</td>

The readonly attribute on HTML input elements actually means that the value is readonly.
You actually want to make a checkbox uncheckable, in that case grab Javascript:
<input type="checkbox" onclick="return false;">

Try disabled="disabled" instead ;)

here i have inserted 7 checkboxes and set their name as c1,c2....c7 in the html page
<input type="checkbox" name="c1" value="english" />
<input type="checkbox" name="c2" value="hindi" />
<input type="checkbox" name="c3" value="gujarati" />
.
.
.
<input type="checkbox" name="c7" value="Marathi" />
here we will use for loop with upto 7 times iteration
for($i=1;$i<=7;$i++)
{
echo $_GET['c'.$i] ."</br>";
}
here 'echo' statement will print values from gettig name of checkboxes c1 ...c7
Here $_GET['c'.$i] will print only its value if that checkboox is checked

Related

can i use multiple radio button in the form like this

hello
i am not being able to submit these value in my controller.
the code is as follows
<input name="status.<?php echo $data['LEAVE_ID']?>" type="radio" class="ace" value="1" checked="" required="" />
<span class="lbl"> YES</span>
<input name="status.<?php echo $data['LEAVE_ID']?>" type="radio" class="ace" value="0"/>
<span class="lbl"> NO</span>
For multiple radio use array in name attribute with id as a index key like this
name="status[<?php echo $data['LEAVE_ID']?>]"
You can achieve it using below code, you just have to add css.
<?php
foreach($dataArrry as $data) {
echo $_POST['status.' . $data['LEAVE_ID']]."<br/><br/>";
}
?>

Use database to check a checkbox

I have a page to view assets with an Edit link. When I click the link it goes to edit_case.php which has a form to edit what elements of the row are in the database as checkboxes. However the boxes do not show them as checked. I have the following code...
// get already checked box values
$repop = "SELECT * FROM case_audit WHERE case_id = $case_id";
$popresults = mysqli_query($dbc, $repop);
$row = mysqli_fetch_array($popresults, MYSQLI_ASSOC);
print_r ($row);
The print_r does show the whole record row from DB. which is either a 1 or 0, checked || not checked.
The form...
<div id="facepics">
<label><input type="checkbox" name="Facial1" value="<?php echo $row['frontrest']; ?>" >Front at Rest </label><br>
<label><input type="checkbox" name="Facial2" value="<?php echo $row['frontbigsmile']; ?>" >Front Big Smille</label><br>
<label><input type="checkbox" name="Facial3" value="<?php echo $row['profile']; ?>" >Profile</label><br>
<label><input type="checkbox" name="Facial4" value="<?php echo $row['subvertex']; ?>" >SubMento Vertex</label><br>
</div>
I know I need to turn the 1's to "checked" just not sure how best to do that.
so basically checked="true" attribute in input creates a checked checkbox.
HTML Code looks like
<input type="checkbox" checked="true">
In your case you can do it like:
<input type="checkbox" name="Facial1" value="frontrest" <?= (intval($row['frontrest']) == 1) ? 'checked' : '';>
Also note that I changed value attribute, with frontrest so that you can identify the checkbox uniquely
EDIT: I have modified the code
<input type="checkbox" name="Facial1" <?=$row['frontrest']==1?'checked':''?>>
I often have the same issue where the browser ignores checked="false" and checks all
so I use
<input type="checkbox" checked>

PHP - Handling HTML checkbox array [duplicate]

I have 1 form in with multiple checkboxes in it (each with the code):
<input type="checkbox" name="check_list" value="<? echo $row['Report ID'] ?>">
Where $row['Report ID'] is a primary key in a database -so each value is different.
How would I be able to tell which checkboxes have been checked? (Maybe multiple)
This is for an inbox system and I have a button below that I want (when clicked) to delete all messages (ids of: $row['Report ID']) which have the checkbox's checked.
Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).
Here's a little sample as requested:
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
?>
Edit To reflect what #Marc said in the comment below.
You can do a loop through all the posted values.
HTML:
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
PHP:
foreach($_POST['check_list'] as $item){
// query to delete where item = $item
}
you have to name your checkboxes accordingly:
<input type="checkbox" name="check_list[]" value="…" />
you can then access all checked checkboxes with
// loop over checked checkboxes
foreach($_POST['check_list'] as $checkbox) {
// do something
}
ps. make sure to properly escape your output (htmlspecialchars())
<input type="checkbox" name="check_list[<? echo $row['Report ID'] ?>]" value="<? echo $row['Report ID'] ?>">
And after the post, you can loop through them:
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $report_id){
echo "$report_id was checked! ";
}
}
Or get a certain value posted from previous page:
if(isset($_POST['check_list'][$report_id])){
echo $report_id . " was checked!<br/>";
}
It's pretty simple. Pay attention and you'll get it right away! :)
You will create a html array, which will be then sent to php array.
Your html code will look like this:
<input type="checkbox" name="check_list[1]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[2]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[3]" alt="Checkbox" value="checked">
Where [1] [2] [3] are the IDs of your messages, meaning that you will echo your $row['Report ID'] in their place.
Then, when you submit the form, your PHP array will look like this:
print_r($check_list)
[1] => checked
[3] => checked
Depending on which were checked and which were not.
I'm sure you can continue from this point forward.

Echoing multiple checkbox values In PHP

I am having trouble using $_GET with radio buttons.
4th<input type="checkbox" name="date" value="4th">
5th<input type="checkbox" name="date" value="5th">
6th<input type="checkbox" name="date" value="6th">
The user chooses what days they are available. Then I want to echo out what days the user selected:
<?php echo "You are available " . $_GET["date"] . "!"; ?>
The above code only echos out one. Not all three. Is there a way to do this?
checkbox values are returned in an array as they share the same index, so you need to use name="date[]" in your HTML.
If you want to know more, just try to print_r($_GET['date']); and see what you get.
And you've tagged your question as radio so would like to inform you that radio and checkbox are 2 different things, radio returns single value, where checkbox can return multiple.
Name will be an array
<input type="checkbox" name="date[]" value="4th" />
<input type="checkbox" name="date[]" value="5th" />
<input type="checkbox" name="date[]" value="6th" />
Then get value like this
<?php
echo "You are available ";
foreach($_POST["date"] as $value) {
echo "$value";
}
?>
You could give each input an id:
<input type="checkbox" id="date1" value="4th" />
<input type="checkbox" id="date2" value="5th" />
<input type="checkbox" id="date3" value="6th" />
Then echo it like this:
$date1 = $_GET["date1"];
$date2 = $_GET["date2"];
$date3 = $_GET["date3"];
<?php echo "You are available " . $date1. ",". $date2. ",". $date3. ",". "!"; ?>
Xth<input type="checkbox" name="date[]" value="Xth">
You can use in php
$_POST['date'][0]
$_POST['date'][1]
Please use array to get multiple value -
Code:
4th<input type="checkbox" name="date[]" value="4th">
5th<input type="checkbox" name="date[]" value="5th">
6th<input type="checkbox" name="date[]" value="6th">
<?php
$date=$_GET['date'];
foreach($date as $dt){
echo "You are available " . $dt . "!<br>";
}
?>
I am not checking above code. I guess it will work.

How to enable or disable check boxes based on a click event?

I have a web app (PHP) and I have to make this change. I am more of a database, scripting guy, please bear with me on this one!
I have 8 check boxes (think numbered 1~8) in a form. I have to implement a condition where in :
If one of the first 4 checkboxes are checked (only one checkbox can be checked in the first 4),
Then the next 4 checkboxes should be disabled
Else the next 4 checkboxes should be enabled.
My solution :
Make the first 4 checkboxes radiobuttons to confirm to the only one
checkbox can be selected condition.
Disable/Enable the next 4 checkboxes based on the above action. So,
if the radiobutton is not selected, then the next 4 checkboxes should
be available for selection.
I have to actually disable the checkboxes rather than hide using jQuery, so the checkboxes should be solidgray (uncheckable) when disabled.
Sample code (stripped off some formatting mess for others looking for a similar solution) :
<div>
<input type="checkbox" name="check1" value="1" id="check1" <?php if (!empty($rows['check1'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check2" value="1" id="check2" <?php if (!empty($rows['check2'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check3" value="1" id="check3" <?php if (!empty($rows['check3'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check4" value="1" id="check4" <?php if (!empty($rows['check4'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check5" value="1" id="check5" <?php if (!empty($rows['check5'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check6" value="1" id="check6" <?php if (!empty($rows['check6'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check7" value="1" id="check7" <?php if (!empty($rows['check7'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check8" value="1" id="check8" <?php if (!empty($rows['check8'])) { echo 'checked="checked"'; } ?> />
</div>
My requests :
What is the most efficient way of doing this? (simple without complicating the problem)
Any sample code is greatly appreciated.
I think this is what you're looking for. You can achieve it using .index() to get current clicked checkbox. .slice() is used to get all elements at index 4 and after.
$('input[type=checkbox]').change(function(){
var $linputs = $('input[type=checkbox]').slice(4);
var $this = $(this);
$linputs.prop('disabled',($this.index() < 4 && this.checked));
if($this.index() < 4 && this.checked){
$linputs.prop('checked',false);
}
});​
FIDDDLE
Or is it something like this that you want? Where only one of the first four checkboxes can be checked. If one is checked then all the others will be disabled.
$('input[type=checkbox]').change(function(){
var $this = $(this);
$(linputs).prop('disabled',($this.index() < 4 && this.checked));
if($this.index() < 4 && this.checked){
$(linputs).prop('checked',false);
}
});​
http://jsfiddle.net/h5wDr/
EDIT:
if you have other checkboxes in the page and want to be able to separate them from this logic, you can add context in the selector so it keeps this code isolated to only those within this div like so
<div id='test'>
<input type="checkbox" name="check1" value="1" id="check1" >first
<input type="checkbox" name="check2" value="1" id="check2" >second
<input type="checkbox" name="check3" value="1" id="check3" >third
<input type="checkbox" name="check4" value="1" id="check4" >fourth
<input type="checkbox" name="check5" value="1" id="check5" >fifth
<input type="checkbox" name="check6" value="1" id="check6" >sixth
<input type="checkbox" name="check7" value="1" id="check7" >seventh
<input type="checkbox" name="check8" value="1" id="check8" >eight
</div>
Then just add the context
var $inputs = $('input[type=checkbox]', $('#test'));
// this will only select checkboxes within the element with id=test
http://jsfiddle.net/h5wDr/2/
<div>
<input type="checkbox" name="check1" value="1" id="check1" />
<input type="checkbox" name="check2" value="1" id="check2" />
<input type="checkbox" name="check3" value="1" id="check3" />
<input type="checkbox" name="check4" value="1" id="check4" />
<input type="checkbox" name="check5" value="1" id="check5" />
<input type="checkbox" name="check6" value="1" id="check6" />
<input type="checkbox" name="check7" value="1" id="check7" />
<input type="checkbox" name="check8" value="1" id="check8" />
</div>
<script>
$(document).ready(function () {
var $firstFourChecks = $("#check1,#check2,#check3,#check4");
var $lastFourChecks = $("#check5,#check6,#check7,#check8");
$firstFourChecks.on('click', function (e) {
var isCheck = $(this).is(':checked');
$firstFourChecks.not($(this)).prop('checked', false);
$(this).prop('checked', isCheck);
if (isCheck) {
$lastFourChecks.prop("disabled", true).prop('checked', false);
} else {
$lastFourChecks.prop("disabled", false);
}
});
});
</script>
This is entirely done in javascript, and is agnostic to the fact you are using php. Essentially, we make sure in the first four for you have not selected, they are set to false. Then we toggle the state of the one clicked.
If you clicked something on in your first four the last four are turned off and disabled, otherwise they are renabled. This matches the posted pseudocode.
You should be able to paste this directly in. The selectors are cached for speed reasons.
http://jsfiddle.net/L4qeN/ see it here.
Edit: wow looks like someone beat me to the punchline by only a few minutes. We did use very different methods; however.
You would have to try it yourself, but I would do something like (using javascript):
Add a class to every checkbox of the first group and another class to every checkbox of the second group;
Check for change events for the first class, turn off all of the first group but the clicked one;
Check if any of the first group is selected and activate / deactivate the second class group accordingly.
Give your first four checkboxes one class and then your second four a second class and then add onclick handlers to all the first checkboxes:
$('.firstfour_class').click(
if $('input:checkbox:checked.firstfour_class').length > 1){
//code to turn OFF the second four and make them unchecked
} else {
//code to turn ON the second four
}
})
Check out the jQuery :checked selector.

Categories