1

As above, I"m trying to create a simple html / PHP page. When the submit button is clicked, I would like different SQL code to be run depending on which of the checkboxes is checked. The SQL code is pretty simple, its just displaying a different table for each checkbox, but I'm not sure how to check if the checkboxes are checked. My HTML code is below

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Ricky Deacon, CSIS3380 Assignment 3. Show Table.html</title>
</head>
<body>
    <h1>Book-O-Rama Catalog Search</h1>

    <form action="showtab.php" method="post">
        <fieldset><legend>Which Tables Would You Like To View?</legend>
            <p>                 
                Customers 
                <input type="checkbox" name="table" value="cust"/>&nbsp; &nbsp; &nbsp;
                Orders 
                <input type="checkbox" name="table" value="ord"/>&nbsp; &nbsp; &nbsp;
                Order Items 
                <input type="checkbox" name="table" value="itms"/>&nbsp; &nbsp; &nbsp;
                Books
                <input type="checkbox" name="table" value="book"/>&nbsp; &nbsp; &nbsp;
                Book Reviews
                <input type="checkbox" name="table" value="brev"/> <br /><br />
                <input type="submit" name="submit" value="Show Tables">
            </p>
        </fieldset>                         
    </form>
</body>

I haven't written the PHP response yet as I am not sure where to start

Thanks

Rick D
  • 139
  • 1
  • 11

3 Answers3

4

Here is a more complete answer of what you'd need to do:

Edited - Also included what your inputs need to look like.

<input type="checkbox" name="table[]" value="cust"/>Orders 
<input type="checkbox" name="table[]" value="ord"/>Order Items 
<input type="checkbox" name="table[]" value="itms"/>Books
<input type="checkbox" name="table[]" value="book"/>Book Reviews
<input type="checkbox" name="table[]" value="brev"/>

<?php
    // Check that the values you're trying to access have actually been posted
    // 'table' is the 'name' of your input
    if (!empty($_POST['table'])) {
        // If it's not empty then set the variable
        $tables = $_POST['table'];
    }
    // If it is empty (Your form didn't submit this input)
    else {
        // end processing or return to the previous page
        return false;
    }

    // You will now need to loop through the array
    foreach ($tables as $table) {
        switch($table) {
            case 'cust':
                // Run Cust SQL Query
                break;
            case 'ord':
                // Run ord SQL Query
                break;
            case 'itms':
                // Run itms SQL Query
                break;
            case 'book':
                // Run book SQL Query
                break;
            case 'brev':
                // Run brev SQL Query
                break;
        }
    }
?>

And for reference why it's better to use a switch case instead of if/else in this situation:

Is "else if" faster than "switch() case"?

giolliano sulit
  • 996
  • 1
  • 6
  • 11
  • giolliano sulit - That response looks like what I need. If I wanted to include the possibility of more than one checkbox being checked, I"m guessing if / else would be more suitable than switch / case, but how would I get the checked value from more than one box... would I need to assign to multiple variables? – Rick D Aug 01 '17 at 23:44
  • Oh ok, it looks like I mis-read that as 'radio' inputs. Because you named all the checkboxes the same, you'd need rename them to, `name[]`, as they'll need to be an array. – giolliano sulit Aug 01 '17 at 23:46
  • @RickD I've updated my answer, that should be what you need, accept the answer if it is :) – giolliano sulit Aug 01 '17 at 23:50
  • Can I do if > if > if statements, rather than else if statements, otherwise I'll need to constantly repeat the same code while checking for an increasing number of checked boxes. I've tried writing the code with if statements (not using else if) but it only displays the table for the last checkbox – Rick D Aug 02 '17 at 04:28
  • The problem with if > if statements is that it may **match multiple** times.. For example if somebody selected `cust` and `order`. The following `if` statements would both return true and run: `if ($table == 'cust') { // do something }` `if ($table == 'cust' && $table == 'order') { // do something }`. Which is not the behaviour that you want to happen. Let me know if you accept this answer, thanks. – giolliano sulit Aug 02 '17 at 04:34
  • Unless I'm missing something, I'm pretty sure that's how I want it to behave. If customers is ticked, it writes the customers table, if orders is also ticked, it writes the orders table below the customers table. At the moment, it is only writing the last ticked table if multiple checkboxes are ticked – Rick D Aug 02 '17 at 04:44
  • @RickD Ok, well then in that case, my first answer would've been more relevant with a slight modification. Check out my updated answer above and accept this answer if you think it's what you need. Thanks – giolliano sulit Aug 02 '17 at 04:52
2

You can check whether a checkbox is checked in showtab.php. The 'value' attribute of the input will be posted to the 'name' attribute when the form is submitted. If the checkbox is blank it will post nothing.

<?php
    if(isset($_POST['table']) && $_POST['table'] == 'cust') {
      // Show table here
    }
    else {
      // Do something else here.
    }    
?>

Your checkboxes will need unique names.

Adam
  • 491
  • 3
  • 16
  • You may not want the else, just several if statements on their own to show multiple different tables. :) – Adam Aug 01 '17 at 23:37
  • Check my answer below, he won't need unique names for each checkbox, he'll just need to pass it through as an array, `name="table[]"`, then in his PHP page loop through. – giolliano sulit Aug 01 '17 at 23:52
0

Hello can get all checked values with $_POST['table'] in your showtab.php.