admin管理员组文章数量:1398778
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.
本文标签: Taking attendance using phpMySQLand JavascriptStack Overflow
版权声明:本文标题:Taking attendance using php, mysql, and javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744394750a2604160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论