-2

This is my first time asking a question on stackoverflow and this question has me stumped. I'm also a beginner at php and html.

<body>
<div class="topnav" id="navbar">
<a href ="user_main_page.php"><img style="max-width:32px; margin-top:-3px;" 
src="/image/logo.jpg"></a1>
<a href="user_main_page.php">Home</a>
<a class="active" href="user_ordernow.php">Order Now</a>
<a href='user_menu.php'>Our Menu</a>
<a href="user_about.php">Who are we?</a>
<a href="user_contact.php">Contact Us</a>
<a style = "float:right" href="../login_page.php">Log Out</a>
</div>

<div class="tab">
<button class="tablinks" onclick="openTab(event, 'Breakfast')" 
id="defaultOpen">Breakfast Menu</button>
<button class="tablinks" onclick="openTab(event, 'Snacks')">Snacks</button>
<button class="tablinks" onclick="openTab(event, 'Drinks')">Drinks</button>
<button class="tablinks" onclick="openTab(event, 'Main')">Main 
Button</button>
<button class="tablinks" onclick="openTab(event, 
'Checkout')">Checkout</button>
</div>

<div id="Breakfast" class="tabcontent">
<?php
    /*declare parameters for $conn*/
    $username="root";
    $password="";
    $database="login";

    $con = mysql_connect("localhost", $username, $password);
    $selectdb = mysql_select_db("login",$con);

    $result = mysql_query("SELECT * FROM menu where category='1'")
    or die(mysql_error());

    echo "<table border='0' cellpadding='10'>";
    echo "<tr> <th>Order</th> <th>ID</th> <th>Product</th> <th>Price ($)</th></tr>";
      // loop through results of database query, displaying them in the table

      while($row = mysql_fetch_array( $result )) {
        echo "<tr>";    // echo out the contents of each row into a table
        echo '<td><input type="checkbox" value="" name="id[]" />' . '</td>'; />' . '</td>';
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . $row['name'] . '</td>';
        echo '<td>' . $row['price'] . '</td>';
        echo "</tr>";
      }

echo "</table>";
?>
</div>
<div id="Snacks" class="tabcontent">
  <h3>snacks</h3>
  <p></p>
</div>
<div id="Drinks" class="tabcontent">
  <h3>Drinks</h3>
  <p></p>
</div>
<div id="Main" class="tabcontent">
  <h3>Main</h3>
  <p></p>
</div>
<div id="Checkout" class="tabcontent">
  <h3>Checkout</h3>
  <p></p>
</div>

It's connected to a mysql database Login and table menu. In php section of the code I access the database and display the database in a table. The problem that I have is retrieving the ticked checkboxes after I press a submit button at the end. How can I, after pressing a submit button retrieve which checkboxes are ticked and store that information to another mysql database? The current layout is such.

webpage design - picture

Thankyou in advance!!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
K. Peter
  • 33
  • 2
  • Checkboxes are UI artifacts. You need to translate them into a one-to-many relationship in your database and update the tables accordingly. – duffymo Apr 27 '17 at 11:10
  • 3
    @duffymo useless answer for a beginner ... – VikingCode Apr 27 '17 at 11:11
  • you'll need to update the page via JS - realistically you need to decouple your retrieve and store (CRUD) php code from your html (view). JS should make an async call to the script(s) which talk to the db, and use those results to update fields. However, thats probably beyond the scope of your ability right now - so instead - simply add an evaluation in your `while` loop and after someone 'submits' the form, redraw the page, the idea being your updated data should be present, and when the evaluation takes place in the `while` loop, your html reflects the data's current state. – Brandt Solovij Apr 27 '17 at 11:12
  • Not useless; good information. It's not an answer b/c I don't have time to put in the effort to show him/her how. Maybe you'd like to show us how @VikingCode. It'll help boost your rep here more than comments. – duffymo Apr 27 '17 at 11:14
  • 2
    @duffymo I don't think this site is about internet points, if you don't have the time for an appropiate answer you shouldn't answer at all – Lars Beck Apr 27 '17 at 11:19
  • what you need to do is to add an hidden input with a name attribute and a form in order to retrieve them through a POST array, then a `WHERE` clause related to that row. There would also be a `foreach` needed for each checkbox selected. – Funk Forty Niner Apr 27 '17 at 11:28
  • I didn't answer. I made a comment. – duffymo Apr 27 '17 at 11:40
  • 1
    Some of the answers below haven't quite grasped what the OP wrote here: *"**The problem that I have** is retrieving the ticked checkboxes after I press a submit button at the end. How can I, after pressing a submit button retrieve which checkboxes are ticked and store that information to another mysql database?"* - That code which is supposed to do the job, isn't posted in the question. One of the answers did to a certain point address this, but did not / could not guess what their query is/should be. – Funk Forty Niner Apr 27 '17 at 11:42
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 27 '17 at 13:04

2 Answers2

3

Assuming all of your fields are named, HTML sends all text boxes to the server, but only selected buttons. For normal buttons and radio buttons, that’s intuitive, but for check boxes, that requires extra work.

In your case, the checkboxes are named id[]. PHP will see that as an array of data:

$_POST['id'][]

(or is it $_GET? I don’t see the form method.)

The problem there is that you won’t know which one is checked, because the array keys only reflect the selected boxes.

It is better if you use the following:

'<td><input type="checkbox" value="" name="id[' . $row['id'] . ']" …

That is, put the id in the array key of the name. You will have a series of names like:

name=id[3]
name=id[4]
name=id[7]

or whatever your ids are.

Then, in PHP, you could run the following:

if(isset($_POST['id'])) {               //  any checked?
    $ids=array_keys($_POST['id']);  //  get selected keys
    foreach($ids as $id) {
        //  process each selected id
    }
}

The trick is:

  • put put id as the key in each checkbox name: name="id[…]"
  • in PHP extract these keys
Manngo
  • 14,066
  • 10
  • 88
  • 110
0

you'll need to update the page via JS - realistically you need to decouple your retrieve and store (CRUD) php code from your html (view). JS should make an async call to the script(s) which talk to the db, and use those results to update fields.

However, thats probably beyond the scope of your ability right now - so instead - simply add an evaluation in your while loop and after someone 'submits' the form, redraw the page, the idea being your updated data should be present, and when the evaluation takes place in the while loop, your html reflects the data's current state -- something like this (im ignoring the js entirely here):

<body>
<div class="topnav" id="navbar">
<a href ="user_main_page.php"><img style="max-width:32px; margin-top:-3px;" 
src="/image/logo.jpg"></a1>
<a href="user_main_page.php">Home</a>
<a class="active" href="user_ordernow.php">Order Now</a>
<a href='user_menu.php'>Our Menu</a>
<a href="user_about.php">Who are we?</a>
<a href="user_contact.php">Contact Us</a>
<a style = "float:right" href="../login_page.php">Log Out</a>
</div>

<div class="tab">
<button class="tablinks" onclick="openTab(event, 'Breakfast')" 
id="defaultOpen">Breakfast Menu</button>
<button class="tablinks" onclick="openTab(event, 'Snacks')">Snacks</button>
<button class="tablinks" onclick="openTab(event, 'Drinks')">Drinks</button>
<button class="tablinks" onclick="openTab(event, 'Main')">Main 
Button</button>
<button class="tablinks" onclick="openTab(event, 
'Checkout')">Checkout</button>
</div>

<div id="Breakfast" class="tabcontent">
<?php
    /*declare parameters for $conn*/
    $username="root";
    $password="";
    $database="login";

    $con = mysql_connect("localhost", $username, $password);
    $selectdb = mysql_select_db("login",$con);

    $result = mysql_query("SELECT * FROM menu where category='1'")
    or die(mysql_error());

    echo "<table border='0' cellpadding='10'>";
    echo "<tr> <th>Order</th> <th>ID</th> <th>Product</th> <th>Price ($)</th></tr>";
      // loop through results of database query, displaying them in the table

      while($row = mysql_fetch_array( $result )) {
        echo "<tr>";    // echo out the contents of each row into a table
        echo '<td><input type="checkbox" value="" name="id[]" '. ($row['selected'] ? .' checked=true '. : .''. ).' />' . '</td>'; />' . '</td>';
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . $row['name'] . '</td>';
        echo '<td>' . $row['price'] . '</td>';
        echo "</tr>";
      }

echo "</table>";
?>
</div>
<div id="Snacks" class="tabcontent">
  <h3>snacks</h3>
  <p></p>
</div>
<div id="Drinks" class="tabcontent">
  <h3>Drinks</h3>
  <p></p>
</div>
<div id="Main" class="tabcontent">
  <h3>Main</h3>
  <p></p>
</div>
<div id="Checkout" class="tabcontent">
  <h3>Checkout</h3>
  <p></p>
</div>
Brandt Solovij
  • 2,124
  • 13
  • 24