I am creating a table based on rows returned from a MySQL query using PHP:
while($row = mysqli_fetch_array($query_results)){
echo "<tr>";
echo "<td>$row[Title]</td>";
echo "<td>$row[Name]</td>";
echo "<td><input class='row-$i' id='one-$i' type='checkbox' ($row[check_one]==1 ? 'checked' : '')/></td>";
echo "<td><input class='row-$i' id='two-$i' type='checkbox' ($row[check_two]==1 ? 'checked' : '')/></td>";
echo "</tr>";
$i++
}
What I want to have happen is if the first checkbox is checked and you check the second one, the first checkbox becomes unchecked. I was originally going to use radio buttons to accomplish this, but the user also needs to be able to freely check or uncheck the first check box (ie both can be unchecked at one time), so radio buttons won't work. So what I need to do is detect change for two-$i elements, but am not sure how to do so for a dynamic element. Under normal circumstances the JQuery would be:
$('#two').change(function(){...});
So how can I modify this to work on the dynamic ids?
function change(val)
{
if(val=='one-1')
document.getElementById('two-2').checked=false
else
document.getElementById('one-1').checked=false
}
First<input type="checkbox" id="one-1" value="one-1" onClick="change(this.value);"/><br>
Second<input type="checkbox" id="two-2" value="two-2" onClick="change(this.value);"/>
This will help you.
Happy Coding.
$(document).ready(function(){
$("#table > input[type='checkbox']").on("click",function(){
$('#table > input:checkbox').not(this).removeAttr('checked');
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="table">
First<input type="checkbox" id="one-1" value="one-1" /><br>
Second<input type="checkbox" id="two-2" value="two-2" /><br>
Third<input type="checkbox" id="two-2" value="two-2" />
</div>
Related
I have tried searched, but I am missing somthing or not phrasing myself correctly I guess.
php, sql and Codeigniter.
What I have is a list of reports in a database.
Theese have a column that states if they are "active" 1 or 0.
What I want to do is add a checkbox on the list that prints the rows in my view as that I can select and then when I have selected the rows I want to be able to press a button and by sending form data change the selected rows in the database to a 1 or 0.
example of how I am thinking of presenting it:
echo "<form action='' method='POST'><table>";
foreach($reports->result() as $row)
{
echo "<tr>";
echo "<td><input type="checkbox" name=""></td>;
echo "<td>".$row->name."</td>";
echo "<td>".$row->time."</td>";
echo "<td>".$row->spot."</td>";
echo "<td>".$row->priority."</td>";
echo "</tr>";
}
echo "<tr><td colspan='5'><input type='submit'></td></tr>";
echo "</table></form>";
What would be the best is if I could get the checkboxes I select into a array for example that would be lovely.
I have tried with array_push, but I could not find a way to select only the ones I selected to send. Is there some way to do this that I dont know of?
I have found a way to make the selected checkboxes into an array by adding id[] to all checkboxes name like this this:
form_checkbox('id[]',$row->id, FALSE);
I loop out alot of these and might get this for example:
<input type="checkbox" name="id[]" value="12340">
<input type="checkbox" name="id[]" value="12353">
<input type="checkbox" name="id[]" value="12367">
<input type="checkbox" name="id[]" value="12389">
<input type="checkbox" name="id[]" value="12391">
Then in my controller I added this where my form takes action to and loops through the ones that I have checked and take action on them.
$arr = array();
if(!empty($_POST['id'])) {
foreach($_POST['id'] as $selected) {
$this->db->where('id', $selected);
$this->db->update('rapporter' , $data);
}
}else{}
This is working great for me. Now I only update the rows I have checked!
If someone have a more efficient way of doing this I am all ears!
im kinda trying to get into programming in general and was wondering how to uncheck / check with updating the array-
like as soon as someone checks a 2nd checkbox it should uncheck the first option and update the search (w the new data)- im a mere beginner and kinda lost rn so would appreciate any form of help
<form action="index.php" id="form1" name="form1" method="get">
<?php $i = 0; foreach ($row_page_nav_kategorie as $row_page_nav_kategorie) { ?>
<label class="checkbox-container">
<input <?php if (strpos($url,$row_page_nav_kategorie['typ']) == true) {echo 'checked="checked"';}?>
type="checkbox"
class="checkmark"
name="hotelKategorie[]"
id="ckb5"
value="<?php echo $row_page_nav_kategorie['typ']; ?>"
onclick="kategorie(<?php echo $i ?>);"
onchange="submit()"/>
<?php echo $row_page_nav_kategorie['typ']; $i++;?>
</label>
<?php } ?>
</form>
You should use radio buttons, but if you want to overwrite checkbox functionality below code will take care of it, I have added a common class on your inputs:
function checkboxClick(obj) {
var cbs = document.getElementsByClassName("checkkbox-option");
for (var i = 0; i < cbs.length; i++) {
cbs[i].checked = false;
}
obj.checked = true;
}
Demo
First of all welcome to the community!
As for your question, there is multiple ways to handle this, one of wich is as followed:
In HTML there's an attribute called radio wich you can add to your input by using type='radio'. In a set of radio buttons, only one can be checked at any time. If you then want to immedietely submit your form, you can use something like onChange='this.form.submit()'. This will submit your form when the value is changed, such as pressing on a different radio button.
Something to keep note of is that the attribute onChange is case sensitive as far as i'm aware. You were heading in the right direction with onchange="submit(), but your code doesn't know what to submit. this.form.submit() will submit the form that the element is in.
Use Radio Buttons or use JavaScript on your page to dynamically uncheck other checkboxes when you click on one.
If you want to dynamically update the page content with the new search results you should also look into AJAX which basically means you will call PHP functions from JavaScript code and those will return JSON arrays that you can exploit to modify your page's DOM.
Try THIS
HTML:
<label><input type="checkbox" name="check1" class="checkbox" /> CheckBox1</label>
<label><input type="checkbox" name="check2" class="checkbox" /> CheckBox2</label>
<label><input type="checkbox" name="check3" class="checkbox" /> CheckBox3</label>
<label><input type="checkbox" name="check4" class="checkbox" /> CheckBox4</label>
jQuery:
$(".checkbox").change(function() {
$(".checkbox").prop('checked', false);
$(this).prop('checked', true);
});
if you want to uncheck the selected checkbox
$(".checkbox").change(function() {
$(".checkbox").not(this).prop('checked', false);
});
hope this helps, thanks !!!
Hi i am having results and its contains a checkbox as <input type='checkbox' name="ChkS[]" value='<? echo $rs->id?>'>
I have a print out list option in the footer as <input type="button" value="Show Printable List" class="butten" onClick="openPrint()">
Its opening the printable list in new pop up window.
function openPrint() {
window.open("<?= $_SERVER["PHP_SELF"]; ?>?<?=$qstring?>&print=true","","width=1024,height=600,menubar=yes,resizable=yes, scrollbars=yes");
}
I am in need to use the above select checkbox and it should show the selected print list in the pop up ?
I hope it should not be problem any1 to understand it. Any help ?
Javascript Modification:
var s = "";
function changeval(value) {
s = value;
}
function openPrint() {
alert(s);
window.open("<?= $_SERVER["PHP_SELF"]; ?>?<?=$qstring?>&print=true","","width=1024,height=600,menubar=yes,resizable=yes, scrollbars=yes");
}
HTML Modification:
<input type='checkbox' name="ChkS[]" value='<? echo $rs->id?>' onchange="changeval(this.value)" />
What I have done?
Created an Global Variable s.
Attached or Bind onchange event on checkbox and set the value of Variable s according.
Now, You have variable s with value, you just have to use it in any function.
Currently you have defined button onclick event , instead of this set onclick event on
checkbox.
<input type='checkbox' name="ChkS[]" value='<? echo $rs->id?>' onClick="openPrint();">
Hope this will help you to solve your problem.
This is the code for the php file containing the form with radio buttons:
<?php
while($r7 = mysql_fetch_array($rt3)){
$name=$r7["name"];
$s=$r7["question"];
echo "
<div>$s</div>
<form name='Tester' method='post' onSubmit='return RegValidate(this);' action='quest.php' >
<input type='radio' name='w1' value='1'>1<br>
<input type='radio' name='w1' value='2'>2<br>
<input type='radio' name='w1' value='3'>3<br>
";
}
echo" <input name='Submit' value='Tester' type='submit' />
</form>";
?>
The $name and $q are derived from the database using a mysql query. This works fine.
I have used the following javascript code so far:
function RegValidate(Tester)
{
if($('#w1').not(':checked'))
{
alert('Radio not checked');
return false;
}
}
But with this code, I continue to get the error message, and I am not able to move on even though I have selected a radio button.
The while loop produces several sets of radio buttons, as $q, gets the question from the database and posts it on the page along with 3 radio buttons. Each question $q, has the 3 radio buttons in the above, hence the reason for the $name.
How do I validate this form with javascript. NB. The form will only contain radio buttons.
function RegValidate(Tester) {
if ($(":radio[name=w1]:checked").toArray().length == 0) {
alert('nothing selected');
return false;
}
}
FIDDLE
you have an error, your input doesn't have ID or CLASS, so add to all of the input radio class='w1' and try the next code:
if ($(".w1 option:selected").length<1)
alert ('nothing selected');
also take a look to this post: Check if option is selected with jQuery, if not select a default
do these things:
<input type='radio' class='w1' name='w1' value='1'>1<br>
<input type='radio' class='w1' name='w1' value='2'>2<br>
<input type='radio' class='w1' name='w1' value='3'>3<br>
And in js:
Latest edit:
if ( $(':radio[class=w1]').attr('checked') != 'checked' ) {
alert('Radio not checked');
}
I am displaying three radio buttons in each row for each employee, how to validate for empty check.
<td><input type="radio" name="stf_att_<?php echo $stf_id ;?>" value="p" /></td>
<td><input type="radio" name="stf_att_<?php echo $stf_id ;?>" value="a" /></td>
<td><input type="radio" name="stf_att_<?php echo $stf_id ;?>" value="l" /></td>
am using another table to display values using id for each employee. have to use name="" for validation but in name am using id from database, how i do with js or php
if(document.getElementById('staff').checked) {
//will come here is a particular radio is checked
}
You can mark one of the radio buttons as checked (checked="checked") in the HTML code, which would guarantee that one radio button is always selected. Since one of the option is true for all the employees, think this would solve your problem.
Add a class name to each of the three radio buttons like their names. (stf_att_<?php echo $stf_id ;?>). And use the function below to validate radio buttons:
function checkRadio(classname) {
var el = document.getElementsByClassName(classname);
for(var i=0; i<el.length; i++) {
if(el[i].checked) {
return 1;
}
}
return 0;
}
It is so much easier using a javascript library like jquery.