0

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
    }
?>
5
  • 1
    once the form is submitted they will be in the get\post array .. your missing a form with a submit and you need to restructure this or use another page to distinguish between before and after form submission Commented May 11, 2018 at 0:03
  • Where do you set $name and $num_recs? The checkboxes aren't in a <form>. Commented May 11, 2018 at 0:04
  • @Barmar sorry that was from another script, edited now Commented May 11, 2018 at 0:05
  • @smith I did have a button afterwards for post, please see my updated code above Commented May 11, 2018 at 0:08
  • How would I be able to access the checked boxes once "submit" is clicked? Which get/post array would they be in? Commented May 11, 2018 at 0:11

2 Answers 2

1

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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

How would I put the checkboxes in another file? Would they be in HTML or PHP? Would love some pointers on the syntax to process the checkbox file in my POST file as well. My apologies, I am new to PHP.
Why would they be in another file? This file is the one that creates the checkboxes. You should do it the way I show in the answer.
0

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

2 Comments

How would I put the checkboxes in another file? Would they be in HTML or PHP? Would love some pointers on the syntax to process the checkbox file in my POST file as well. My apologies, I am new to PHP.
In your file named form.html you add all HTML form details and submit it to another file, like process.php through <form action="process.php" method="post">' . You add your logic to check for the $_POST` to that file.

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.