Taking attendance using php, mysql, and javascript - Stack Overflow
Simply what I need to do is to have a page where there is a list of names and check boxes right next to them. Once you click the submit button at the bottom I need to traverse through the page and find each one that is checked. With that information I have to insert those names today's date and a 1 into a table in mysql.
The problem is getting the button onclick to execute some php to update the table. I believe there is a way to post to a php file some information so if I could get an array of all the checked people and pass that to the php file where it will then do they mysql queries that would be great.
I have looked around and everyone suggests one thing or another but no one gives any sort of example or even a decent description of what would need to be done.
I already have the names printed out and the checkboxes but I need some way of submitting the attendance.
Any suggestions?
Simply what I need to do is to have a page where there is a list of names and check boxes right next to them. Once you click the submit button at the bottom I need to traverse through the page and find each one that is checked. With that information I have to insert those names today's date and a 1 into a table in mysql.
The problem is getting the button onclick to execute some php to update the table. I believe there is a way to post to a php file some information so if I could get an array of all the checked people and pass that to the php file where it will then do they mysql queries that would be great.
I have looked around and everyone suggests one thing or another but no one gives any sort of example or even a decent description of what would need to be done.
I already have the names printed out and the checkboxes but I need some way of submitting the attendance.
Any suggestions?
Share Improve this question asked Nov 3, 2011 at 3:28 ericeric 4542 gold badges9 silver badges21 bronze badges4 Answers
Reset to default 2You can do this solely in PHP unless you need the JavaScript to update other form elements as the user interacts with the page. So you can forget about the onclick event. What you need to do is link to a .php file in your form's action. So <form action="some_php_file.php" method="post">
. Now in that some_php_file.php file, you'll grab the $_POST info from the form and build an array. So if your checkboxes look like this, <input type="checkbox" name="name1" value="Some Value" />
then you'll build your array by checking that !empty($_POST['name1'])
. If it's not empty, then the user checked the checkbox. Now that you have your array with all of the checked checkboxes, you can insert this along with your other info into the DB with MySQL.
Create a form on the page with all your checkboxes:
<form action='this_page.php' method='post'>
David Smith <input type='checkbox' name='student[davidsmith]' value='1' />
Suzy Cutie <input type='checkbox' name='student[suziecutie]' value='1' />
Tom Geralds <input type='checkbox' name='student[tomgeralds]' value='1' />
<input type='submit' value='Submit' name='submit_button' />
</form>
Your PHP code, located in the header of the same file, or in a separate (included) file would be something like:
<?php
if(!isset($_POST['submit_button'])){
//form has not been submitted
return;
}
//form HAS been submitted
$student_array = $_POST['student'];
foreach($student_array as $name=>$val){
//$val needs to be 0 or 1.
$val = ($val == "1")?1:0;
mysql_query("INSERT INTO attendance VALUES('$name',CURDATE(),$val)");
}
?>
Obviously, make sure your number of checkboxes matches the database. The most plete/correct way to do this would be to dynamically create the checkboxes based on values in the database. You should have a 'student' table with their name and a primary key (unique identifier).
Have you tried using a form? I think that's exactly what you're looking for.
Other answers are also good answers but here is my different approach.
You say that you already put the names and checkboxes. OK. Now put a button on the page wherever you want. And then associate it with a click event. If you use Jquery here you can easily get the values of the checkboxes with corresponding names. Have all the information regarding the checkboxes, put them in an array in javascript and then post them to a php file. You could use JQuery's $.post method if you like. I can provide solid code examples if you want.
- Rambus联手微软:研究量子计算内存
- 外媒称谷歌正筹划自主开发安卓手机
- .NET开源:微软"云为先"战略的全面铺开
- Spring Boot 3: Exclude REST Endpoints from Authorization - Stack Overflow
- google tag manager - GTM custom template - not able to trigger callback from script - Stack Overflow
- angular - Dynamic Data in Add-to-calender-button - Stack Overflow
- visual studio code - Why am I getting “Type annotations can only be used in TypeScript files” with tsx files? - Stack Overflow
- stata - Fill in missing values by group for all the variables in the dataset - Stack Overflow
- ggplot2 - alluvial diagram in R, Error: Data not in recognizable format - Stack Overflow
- Flutter : Privacy screen implementation - Stack Overflow
- In Visual Studio 2022, Can not export and import new Project Template - Stack Overflow
- swift - How to set axis order in AxisMarks in SwiftUI Chart view - Stack Overflow
- artificial intelligence - ImportError in Python when using the phi library - Stack Overflow
- pine script - Is there a way to incorporate dynamic commission fees in the TradingView Strategy Tester? - Stack Overflow
- visual studio code - VSCode support fully integrated IPython terminal - Stack Overflow
- system verilog - Do delta cycles occur at intermediate stages in SystemVerilog? - Stack Overflow
- python - How to optimise this simulation of a lottery? - Stack Overflow