1

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?

4 Answers 4

2

You 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 complete/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).

11 Comments

I liked everyone's response but I think this is the one I'm going to try. Thank you for the very clear and thorough example it is greatly appreciated.
I have an issue. My table name is attendance with columns for the MemberName, Date, Present. Where Present is a 1 or 0. I want to input the data in total so have the statement be something like INSERT INTO attendance (MemberName, Date, Present) VALUES($row[MemberName], Date(Y-m-d), $val); where the $row[MemberName] is obtained by another sql query getting all the distinct names from my table. They are printed out in that order by the way. Is there a way I can get the $val to be the 1 or 0 that is needed to mark them as present? So far using the foreach you suggested but that isn't working.
@eric - I made changes to the foreach loop, please check it out and get back to me.
sadly this isn't actually inserting anything into my database. I'm not sure if this is on your code end or my database end. I don't see any errors but nothing is being added to my database. I'll try this in an external php file
@eric - the code is not really complete. You need to make sure the proper database connection is being made, and that your date field is set properly, and if your memberName is a foreign key from another table, that the values match. This code is only a guideline for how to complete the task.
|
1

Have you tried using a form? I think that's exactly what you're looking for.

1 Comment

how would the form exactly work though. This would be 20+ names and one submit button. Would I be able to take an array and post it using the submit?
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.