Select All Checkbox from MYSQL - php

Today I'm currently using "Checkbox" and just a little problem. I have a dropdown list in my first page, I choose the items "DropDown 1" and then I click the Submit button after that it will go to the next page. So then, the second page will load all items under the "DropDown 1" using a checkbox only.
My problem is:
How can I check them all by using a checkbox "Check All", where the items in "DropwDown 1" are came from my database [MySQL].
Here's my code in page two:
<input type="checkbox" name="all" id="all" /> <label for='all'>All</label>
<?php
$dropdown_value = (string)$_POST["id"];
echo "<br/>";
if ($dropdown_value == 'All Building')
{
$all = mysql_query("SELECT fldBldgName FROM tblbuildings");
while ($row = mysql_fetch_array($all))
{
echo "<tr><td>";
echo "<input type='checkbox' name='play[]' class='chk_boxes1' value='" . $row['fldBldgName'] . "'>";
echo $row['fldBldgName'];
echo "</td></tr><br/>";
}
}
?>
<script>
$(document).ready(function(){
$('input[name="all"]').bind('click', function(){
var status = $(this).is(':checked');
$('input[type="checkbox"]').attr('checked', status);
});
});
</script>

look checked Attribute
echo "<input type='checkbox' class='chk_boxes1' name='play[]' value='".$row['fldBldgName']. "' checked>";
and why a <br> in a table?
EDIT
try this:
echo '<div><input type="checkbox" class="checkall"> Check all</div>';
$all = mysql_query("SELECT fldBldgName FROM tblbuildings");
while ($row = mysql_fetch_array($all))
{
echo "<div><input type='checkbox' name='play[]' class='chk_boxes1' value='" . $row['fldBldgName']."'>";
echo $row['fldBldgName'];."</div>";
}
and jQuery function:
$(function () {
$('.checkall').on('click', function () {
$(this).closest('fieldset').find(':checkbox').prop('checked', this.checked);
});
});
http://jsfiddle.net/H37cb/210/

Try listening to the change event on the .chk_boxes element. You should also use prop instead of attr when working with "binary" properties like `checked':
$('.chk_boxes').on('change', function(){
$('.chk_boxes1').prop('checked', $(this).prop('checked'));
});
http://api.jquery.com/prop/
You also need to wrap all your tr/td elements in a table element.:
<table>
<?php
...
?>
</table>
There's no need for br elements after a tr. tr will always start on a new line.

Related

Dynamic Input Values Not Recognized By Php

I have a table where i dynamically add rows with input fields based on the user requirement. These input fields are stored as an array so that looping over them can get the values in each row to insert into the db. The issue is that only the first "static" row from the table is recognized in the array. Every other dynamic input field is not stored in the input array when submitted. A snippet of the table code is
<tr><td><input type=number name=minimum[] required></input></td><td><input type=number name=maximum[] required></input></td> <td><input type=number name=overall[] required></input>
<td><?php echo $formular;?></td></tr>
</tbody><tfoot><tr><td colspan="25"><div class="text-center">
<button type="submit" class="btn btn-success">Submit</button>
</div></td></tr></tfoot></table></div></form>
The jquery code to add an extra row is:
<script>
$(window).load(function(){
$(function () {
var rowv="<tr><td><input type=number name=minimum[] required></input></td>
<td><input type=number name=maximum[] required></input></td><td><input
type=number name=overall[] required></input></td><td><?php echo $formular;?>
</td></tr>";
$("#addRow").click(function () {
var row = $(rowv);
$("#mt > tbody").append(row);
});
});
});
</script>
The (abbreviated) php code to process the array is :
foreach($_POST['fi'] as $key=>$value){
$sql="insert into band(fi) values ('$value')";
}
Only the first row is being inserted. Any row that added dynamically is not submitted to the post array. How can i resolve this?
Create your input field like this and then append it to the form and then try.
var input = document.createElement("input");
input.type = "text";
input.name = "minimum[]";
input.required = "true";
$("#testForm").append(input);
Your jQuery syntax seems incorrect. Change that click function to this:
$("#addRow").click(function () {
$("#mt > tbody").append(
"<tr>" +
"<td><input type='number' name='minimum[]' required></td>" +
"<td><input type='number' name='maximum[]' required></td>" +
"<td><input type='number' name='overall[]' required></td>" +
"<td><?php echo $formular;?></td>" +
"</tr>"
);
});

Getting the value of a dynamically created input

I have following jquery script which load a form into a div "blankform"
jQuery('#metentry').submit(function(e){
e.preventDefault();
// getting data from form to variables
var date = jQuery('#stddate').val();
var kgsunit = jQuery('#unit').val();
var kgsshift = jQuery('#shift').val();
//sending data to script
jQuery.post("get_blank_form.php", {
"date": jQuery('#stddate').val(),
'unit': kgsunit,
'shift': kgsshift,
}, function(data) {
//loading a form to the div blankform
jQuery('#blankform').html(data);
});
get_blank_form.php contains following code
<?php
echo'<form id="shiftentry" name="shiftentry" >';
echo "Date:<input id=shiftdate value='".$date."'/>";
echo "Unit:<input id=shiftdate value='".$unit."'/>";
echo "Shift:<input id=shiftdate value='".$shift."'/><br/>";
echo "<table><tr><th>No</th><th>Ele</th><th>Location</th><th>Dose Rate (mGy/h)</th><th> Tritium (DAC)</th> <th>Part (DAC)</th> <th>Iodine (DAC)</th><th> Surface Cont. (Bq/cm2) </th></tr>";
while($row2 = mysql_fetch_array($result_sec))
{
echo "<tr>";
echo "<td>".++$rc."</td>";
echo "<td><input size='5' id='".$row2['elevation']."' value='".$row2['elevation']."' /></td>";
echo "<td><input id='".$row2['loc_id']."' value='".$row2['location']."' /></td>";
echo "<td><input size='5' id='dose".$rc."' value='".$row2['radn_level']."'/></td>";
echo "<td><input size='5' id='h3".$rc."' value='0' /></td>";
echo "<td><input size='5' id='part".$rc."' value='0' /></td>";
echo "<td><input size='5' id='iod".$rc."' value='0' /></td>";
echo "<td><input size='5' id='cont".$rc."' value='0' /></td>";
echo "</tr>";
}
echo " </table>";
echo '<div align="center">';
echo '<input type="submit" id="submit" name="submit" value="Submit" width="30" />';
echo '</div>';
?>
this will create a form which will have input like id1, id2 ... id25, dose1, dose2 ... dose25 and so on.
I want to read the values of these input boxes. Normal method like var loc1 = $("#id1").val(); is not working since these elements are dynamically created.
Even the submit button clicking even is also not triggered.
the script below do not produce any output.
jQuery('#shiftentry').submit(function(e) {
e.preventDefault();
alert("Submitted");
Any suggestions please?
Since the Form DOM is dynamically created after Ajax Call, You can use .on ?
like:
$(document).on('submit' , '#shiftentry' , function(e) {
e.preventDefault();
alert("Submitted");
});
You will need to use on() method for event handling as the form is created using ajax call and added later.
As it's added later, it will not be bound to submit event handler that you had specified. To bind it with the handler you need to use on() from jQuery.
$(document).on('submit', '#shiftentry', function() {
//Your code
});
As for the submit button, you can trigger it using
$('#submit_id').on('click', function(){
$('#shiftentry').send(function(e) {
e.preventDefault();
alert("Submitted");
);
})
as for getting the values using.
$('#id1').val()
this should just work just fine but remember that some characters are not permitted in id field such as [, ], . etc.. if you cant avoid this special characters in your id field you can use.
$('input[name="value"]').val();
to get the value of your fields.

why is jquery not passing a post value

I have a page with two forms on it. This shouldn't be a problem, but it is not working. My jquery looks like this:
$(document).ready(function()
{
$("table.jobs tbody tr td#ejob").click(function()
{
var $this = $(this);
var col = $this.text();
var LeftCellText = $this.prev().text();
$('#jobid').val(LeftCellText);
if (col == '')
alert("Please pick another column");
else
$('#jobUpdate').submit();
});
var now = new Date();
var month = now.getMonth()+1;
var day = now.getDate();
var year = now.getFullYear();
if (month<10){month='0'+month}
if (day<10){day='0'+day}
var now = month +"/"+ day + "/" + year;
var calendarBox = $('#calDate');
//$('#calDate').val(calendarBox.datetimepicker('getDate'));
var thisone = $('#calDate').val(calendarBox.datetimepicker('getDate'));
calendarBox.datepicker(
{
minDate:('setDate', '' , new Date()),
defalt:calendarBox.val(now),
onSelect: function (selectedDateTime)
{
$('#calDate').val(calendarBox.datetimepicker('getDate'));
alert("Sendeing " + $('#calDate').val(calendarBox.datetimepicker('getDate')));
$('#calendarForm').submit();
}
});
}
);
and my forms look like this:
echo "<form id='calendarForm' action='view_rev8.php' method='POST'>";
echo "<table border='0' width='100%'class='calendarTable'>";
echo "<tr><td align='left'>Click on a job number to update or change job information</td>";
echo "<td align='right'></td>";
echo '<td align="right">Calendar: <input type="text" id="calDate" size="15" name"calDate"/></td></tr>';
echo "</table>";
echo "</form>";
//creates the form to post jobid for updateJobForm.php
echo'<form id="jobUpdate" action="../forms/updateJobForm.php" method="post">';
echo'<input type="hidden" name="jobid" id="jobid">';
echo '</form>';
The first function handles when a user clicks on a table item and submits the post value without a problem. The datepicker works and the form gets submitted, but i get the following error: Undefined index: calDate from the following page:
$pickedDate = $_POST['calDate'];
include_once("../php/job_class.php");
echo $pickedDate;
$obj=new jobs();
echo "<div style='border:solid 1px '>";
foreach ($obj as $val)
{
echo "<pre>";
echo print_r($obj);
//echo $obj->jobInfo();
echo "</pre>";
echo "</div>";
}
Why is this not working? It is a simple post from a well defined form, and as far as I can see all fields are named consistently and accurately.
You have invalid HTML in the input tag. (It's missing an = sign.)
<input type="text" id="calDate" size="15" name"calDate"/>
should be:
<input type="text" id="calDate" size="15" name="calDate"/>
So your JS is setting the value since you're using the ID, but the name is what is used for posting a form, which is why it is empty.

How to alert checkbox dynamically from a php while loop to jquery function

In my page i am iterating values based on the database table. I have to give a alert message when i check my radio button residing in the php while loop, when users click on it.
I tried the below code, but its not working. When i am clicking on any of the radio button from my page then , i am not getting alert message . Please help me to solve my problem.
my php code:
<?php $result = mysql_query("SELECT * FROM group_expenditure_details where creater_id =
'$uid'");
$a=0;
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td width='20%' align='center'><input type='checkbox'
name='test".$a++."' id='test".$a++."' value='1' /></td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysql_close($con);
?>
My Jquery code:
<script type="text/javascript">
$(function(){
<?php $od=$_SESSION['id']; $result1 = mysql_query("SELECT * FROM
group_expenditure_details where creater_id = '$od'"); $a=0;
while($row = mysql_fetch_array($result1)) { ?>
$('<?php '#test'.$a++?>').click(function(){
<?php } ?>
alert('clicked');
});
});
</script>
$(function()
{
$('input[id^="test"]').on('click',function()
{
alert('Clicked');
});
});
what $('input[id^="test"]') this will do is select all the input which id is starts with test.
.on is replacement of .live() and .delegate() in new version of jQuery.
.on() works on dyanmically loaded content so if checkbox added later then click event will also work on newly added DOM.
I'll add a class attribute to the radio buttons
<input type='checkbox' class="my-radio" name='test".$a."' id='test".$a."' value='1' />
Then
$(function(){
$('.my-radio').click(function(){
alert('Clicked');
});
});
$(function(){ //ensure dom is ready
$('input[id^=test]').click(function(){ //id starts with test
alert('Clicked');
});
});
Don't use any PHP in your javascript for this. The way you construct your id='test'.$a will ensure that the id `always starts with test. So within that click function above, we have handled the pattern.

Javascript select all button in php

Hi All First of all I don't know Javascript, I got this script from the internet but can't get it to work. If someone can please be so kind and assist me. I'm trying to create a "check all" & "uncheck all" button on my form for my checkboxes, if I click the button it does not select anything. Where am I going wrong?
<?php
session_start();
?>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
<!-- Begin
function CheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}
function UnCheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
// End -->
</script>
</head>
And then the form:
$sql0 = "SELECT * FROM .....
$result0 = mysql_query($sql0);
echo "<form name='myform' action='...' method='post'>";
while($row0 = mysql_fetch_assoc($result0)) {
$test_id=$row0['id'];
$test_name=$row0['test_name'];
echo " <table width=600px>";
echo " <tr>";
echo " <td width=30px><input name='question[$test_id][]' type='checkbox' value='1' /></td>";
echo " <td width=580px align=left>$test_name</td>";
echo " </tr>";
echo " </table>";
}
echo "</P>";
echo "<input type='button' name='Check_All' value='Check All' onClick='CheckAll(document.myform.check_list)'>";
echo "<input type='button' name='Un_CheckAll' value='Uncheck All' onClick='UnCheckAll(document.myform.check_list)'>";
echo "</form>";
if you don't know javascript, i'd strongly recommend you investigate jquery - a library which eliminates cross browser issues and makes a huge amount of javascript far easier.
simply include this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
and your code
<input type='button' name='Check_All' value='Check All' onClick='$(":checkbox").attr("checked",true);'>
untested, but should be okay.
I'd highly recommend using jQuery, it allows you to focus on the logic and not worry about cross browser issues you might run into.
<script src='http://code.jquery.com/jquery-1.6.3.min.js' type='text/javascript'></script>
<script>
$(document).ready(function () {
//check all
$('input[name="Check_All"]').click(function () {
//for all checkboxes where the name begins with "question", check them
$('input[name^="question"]').attr('checked', true);
});
//uncheck all
$('input[name="Un_CheckAll"]').click(function () {
//for all checkboxes where the name begins with "question", uncheck them
$('input[name^="question"]').attr('checked', false);
});
});
</script>
Here's the breakdown...
Select all buttons on the page where the name attribute is set to Check_all
$('input[name="Check_All"]')
When the found item(s) are clicked, execute the code in the function that is passed
$('input[name="Check_All"]').click(function () {
//javascript code here
});
for all checkboxes where the name begins with "question", check them
$('input[name^="question"]').attr('checked', true);
Change this:
<td width=30px><input name='question[$test_id][]' type='checkbox' value='1' /></td>";
to this:
<td width=30px><input name='check_list[]' type='checkbox' value='1' /></td>";

Categories