Jump to content

Using Multiple Checkboxes


Red Moon

Recommended Posts

I have encountered a brick wall trying to use checkboxes. I am attempting to create a small form where I can record if a customer's item has been sent. The way I intend it to work is that the name and ID of the customer (stored in the 'customer' table) are retrieved through a query and displayed in two columns. In the third column is a checkbox beside the name and ID of each customer which, when ticked, sends a value to a second table called 'sentorders' for that particular person. As I am just trying to understand the basics of this for now, a list of all customers will be shown for the time being. The default value of the checkbox needs to be 'No' and if the checkbox is ticked the value should become 'Yes'. When a checkbox is ticked, the customer name, ID and current date are sent to the 'sentorders' table. Only if the box is ticked will the details be stored in the table.

 

The problem I having is how do I set up the checkbox to work as described above. I have had a look at other questions online, but don't understand how it works. Any help would be appreciated. My current code is as follows.

 


<?php
$query = mysqli_query ($connect, "SELECT name, id FROM customer"); 

// Create Table
echo '<table align="left">
	  <tr>
		<td align="left"><b>Name</b></td>
		<td align="left"><b>ID</b></td>	
		<td align="left"><b>Order Sent</b></td>
	  </tr>';		  

// Show Name/ID
while ($row = mysqli_fetch_array($query)) {
	echo '<tr><form action="order_sent.php" method="post"><td align="left">
			<td align="left">' . $row['name'] . '</a></td>
		    <td align="left">' . $row['id'] . '</td>
			<input type="checkbox" name="order[];" value=''></tr>';	

		}				
echo '</table><p align="left"><input type="submit" name="enter" value="Update"/><input type="hidden" name="enter" value="TRUE" /></form>'; 		
}
?>

Link to comment
Share on other sites

Problem #1:

echo '<tr><form action="order_sent.php" method="post"><td align="left">

U have this in the while loop, yet the submit button is outside of the while loop.

Fix: I suspect that this is one form, so this line should be above the while loop.

 

Problem #2

<input type="checkbox" name="order[]" value=''></tr>'

 

You show the name and id of the particular row, however when it comes to the forum input, u don't specify to which id this checkbox belongs to. U have to let php know somehow.

 

Checkboxes are funny thing, if left uncheck they don't return a name/value pair for the checkbox, when checked, the name/value pair is sent

 

Fix:

<input type="checkbox" name="order[]" value='{$row['id']}'></tr>'

 

So when u do check a number of orders, php will have $_POST['order'] as an array with selected id's.

 

but always check if it's set before U add it to a variable

if(isset($_POST['order'])
   $order=$_POST['order]
else
  $order=array();

Link to comment
Share on other sites

So it should look like this?

 

if(isset($_POST['order'])) {
		   $order= ($_POST['order'][0]);
		} else {
		  $order=array();
		  }

 

After that, is it then just a case of using an INSERT INTO query to have each individual customer added to the database?

Link to comment
Share on other sites

no the code i had is fine

remember it's an array

so $_POST['order'] will hold array ( 1,5,7,9 )

or if one order array(5)

 

the elements are the order id

so u can update of the db either by inserts if using a seperate table

or  update if its the field is attached to an already existing table

foreach($orders as $orderid)
  mysql_exec("INSERT INTO orders_sent (id,senton) VALUES($orderid,NOW())");

 

or

$order=implode(',',$order);
mysql_exec("UPDATE ORDERS senton=NOW() WHERE id IN ($order)");

 

$order is an array, so u access each element as you would a normal array.

 

Link to comment
Share on other sites

I would probably check to see if the specific element is set. If it isn't, and I needed to use the value of $order in a database query, I would  just initialize it as an empty string.

 

if( isset($_POST['order'][0]) ) {
     $order = $_POST['order'][0];
} else {
     $order = '';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.