2

I'm trying to write an attendance system that, when a user is present at a class, the staff will check a box and the program will then add one to the present count of the relevant customers. The problem is to output the register, taken from phpMyAdmin, it uses a while loop so all the checkboxes have the same variable name. This is the code I have so far...

echo "<form 'action=badminreg.inc.php' method='post'>";
      while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr><td>".$row['bookingID']."</td><td>".$row['firstName']."</td><td>".$row['surname']."</td><td><input type='checkbox' name='present' value='present'</td><td><input type='checkbox' name='incident' value='incident'</td></tr>";
      }

      echo "<input type='submit' name='reg-submit'>";
      echo "</form>";
      isset($_POST['reg-submit']);
      $pres = $_POST['present'];

I need to separate the check box inputs so that the program will be able to mark individual users differently to others. I'm sort of new to PHP as this is for my A-level coursework so is there a way to get around this? Any help would be great. Thanks

2 Answers 2

1

You can define the name for the checkbox elements as array and pass the bookingID as value.

Example (untested):

$output = "<tr><td>{$row['bookingID']}</td><td>{$row['firstName']}</td><td>{$row['surname']}</td><td><input type='checkbox' name='present[]' value='{$row['bookingID']}'</td><td><input type='checkbox' name='incident[]' value='{$row['bookingID']}'</td></tr>";

The variables $_POST['present'] and $_POST['incident'] contains then an array with the IDs of the selected check boxes.

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

Comments

1

If you change it to name='present[]' then when you submit the data again, you'll get an array of values inside $_POST["present"] instead of a single one. (You'll get one item in the array for each checkbox with that name which was actually checked - a quirk of HTML forms is that if the checkbox wasn't checked, its value is not submitted at all).

You'll also want to change the value of the checkbox to be the ID of the customer (or booking, maybe), so you can identify which box was checked.

Same for the "incident" checkbox as well, of course.

So you're aiming for something like this, I think:

echo "<tr>
  <td>".$row['bookingID']."</td>
  <td>".$row['firstName']."</td>
  <td>".$row['surname']."</td>
  <td><input type='checkbox' name='present[]' value='".$row['bookingID']."'</td>
  <td><input type='checkbox' name='incident[]' value='".$row['bookingID']."'</td>
</tr>";

5 Comments

"Notice: Undefined index: present[] in /opt/lampp/htdocs/coursework/activities/badminton.reg.php on line 29 Notice: Array to string conversion in /opt/lampp/htdocs/coursework/activities/badminton.reg.php on line 30" This is an error I get when trying to set it to a new variable using an isset function
@JonnyStreatfeild-James, present[] should be part of your HTML code, but not part of your PHP.
@JonnyStreatfeild-James if you're getting an error like that, you can't have copied it exactly as I've written it. It works fine - demo: sandbox.onlinephpfunctions.com/code/… . Take care of the specific details when you're coding!
That is what I have and that works fine. But because it's within a while loop, when I try to fetch the results using the isset function, it doesn't read it correctly. It says it has an undefined index: "present[]"
@JonnyStreatfeild-James are you talking about when you try to retrieve the results after submitting the form? As I mentioned in my answer, your results will still be within $_POST["present"], but now they'll be an array, instead of a single item. I didn't suggest to change it to $_POST["present[]"]. Again - details! :-). (Explanation: The [] is just an indication to the browser that it should look for multiple items named "present" when compiling the form data in order to submit it to the server. And the server then knows to compile all the incoming data in a list under that one name)

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.