Lily
Lily

Reputation: 5

How to get checkbox values from within a MySQL query fetch loop in PHP?

I have a PHP script for a website I am building, and it is communicating with a MySQL database have set up. I want to display the courses in one of my database tables as a list of checkboxes.

After a lot of trial and error, I managed to do that. It is in the while loop below, using mysqli_fetch_array().

Now I am stuck on how to actually get the checked values and save only the checked courses to another table in my database (courses taken). As you can see, I select from the DB and output all checkbox lines in one loop. But I need to also check after I output all of them which ones the user has selected.

I tried saving all the $row values to a row array and accessing that twice, but it did not work at all (tons of errors). I want to know the best way to go about this.

Here's my code:

<form action='' method='POST'>
    <input type="submit" name="no_courses_button" value="No"><br>
</form>

<form action='' method='POST'>
    <input type="submit" name="yes_courses_button" value="Yes"><br><br>
</form>

<?php 
    if(isset($_POST['no_courses_button'])){ //if the user has no courses taken so far
        echo "Thank you. Please press NEXT below to proceed."."<br>"; #no need to insert in DB
    }

    if(isset($_POST['yes_courses_button'])){ //if the user has taken courses taken so far
        session_start();
        $netID = $_SESSION['curr_user'];

        echo "Please select the courses you have taken so far."."<br>"."<br>";

        include 'php_mysql_connect.php'; //retrieving all course from DB
        $result = mysqli_query($connect, "SELECT subject, course_number, course_title FROM course ORDER BY course_number");
        error_reporting(0);
        $num_courses = 83;

        while ($row = mysqli_fetch_array($result)) { //HERE's MY PROBLEM AREA
            echo 
            "<input type='checkbox' name='checkbox' value='$row[0] $row[1] $row[2]'>$row[0] $row[1] - $row[2]"."<br>"; 
            //creating checkboxes
        }
    }               
?>

<form action='' method='POST'>
    <br><br><input type="submit" name="submit_button" value="Save Courses"><br><br>
</form>

<?php
    if(isset($_POST['submit_button'])){ //submitting checked list
        echo "Your courses have been saved. Please press NEXT to proceed."."<br>"."<br>"; 
        //I WANT TO CHECK FOR CHECKED BOXES AND INSERT HERE INTO THE DB
    }
?>

Upvotes: 0

Views: 1873

Answers (2)

Paulo Hgo
Paulo Hgo

Reputation: 860

The <form> you have above only has the submit button in it. You need to include the checkboxes as well in the form if you want to verify them. You need to give each one of them a name property that you can refer when retrieving their value (checked or not checked). For example:

<input type='checkbox' name='checkbox1'>
<input type='checkbox' name='checkbox2'>

You need to have unique names for every checkbox and they need to be inside your form.

It's cleaner to separate your POST processing code from the file that has the form in it. In the POST processing file, you need to loop through all posted values and then from their names, figure out which ones have been checked or not and do whatever processing you need (populate another DB table, for example).

Upvotes: 0

Barmar
Barmar

Reputation: 780861

The checkboxes need to be in another form. You can then check for the value of its submit button, and process the inputs.

You also need to give the checkboxes an array-style name. Then the $_POST element will be an array of values that you can loop over.

In my code below, I've changed the value of the checkbox to just contain the course number, that should be enough to identify it when inserting (I assume that's the unique ID of the row).

if(isset($_POST['yes_courses_button'])){ //if the user has taken courses taken so far
    session_start();
    $netID = $_SESSION['curr_user'];

    echo "Please select the courses you have taken so far."."<br>"."<br>";

    include 'php_mysql_connect.php'; //retrieving all course from DB
    $result = mysqli_query($connect, "SELECT subject, course_number, course_title FROM course ORDER BY course_number");
    error_reporting(0);
    $num_courses = 83;

    ?>
    Please select your courses<br>
    <form action='' method='POST'>
    <?php
    while ($row = mysqli_fetch_array($result)) {
        echo "<input type='checkbox' name='coursenum[]' value='$row[1]'>$row[0] $row[1] - $row[2]"."<br>";
    }
    ?>
    <input type="submit" name="choose_courses" value="Save Courses">
    </form>
    <?php
}

if (isset($_POST['choose_courses'])) {
    foreach ($_POST['coursenum'] as $course_number) {
        // insert this course number into database
    }
}

Upvotes: 1

Related Questions