Jump to content

Multiple tick-box select


Mutley

Recommended Posts

I have a list and what I would like to do with this list is have a tick-box next to each one, so someone can select multiple things out of the list. Then, once they click "submit", it displays just the things they selected.

 

The list in the data is displayed from a database by repeating the rows, so if there are 3 rows, it displays it in a table repeating the <tr><td><?=$thing?></td></tr> until it ends.

 

I know how to do forms, just not how to post only the selected data, to display it on a different page.

 

Thanks.

 

 

Link to comment
https://forums.phpfreaks.com/topic/42031-multiple-tick-box-select/
Share on other sites

Only selected checkbox values are sent so you can do this

 

<?php
if (isset($_GET['thing'])) {
    echo "You selected ";
    foreach ($_GET['thing'] as $item) {
        echo " <br />$item";
    }
}
?>
<form>
Thing 1 <input type='checkbox' name='thing[]' value='Thing1'><br>
Thing 2 <input type='checkbox' name='thing[]' value='Thing2'><br>
Thing 3 <input type='checkbox' name='thing[]' value='Thing3'><br>
<input type='submit' name='action' value='Submit'>
</form>

My form is like this:

 

{All the items variables to display} <input type="checkbox" name="order" value="<?=$id?>" />

 

Then when they click submit it goes to newdisplay.php

 

In which that file grabs the $id of all the items chosen. So, I know how I display the items information using the $id, just don't know how "newdisplay.php" knows all the $id's ? How do I carry them across?

If you want to pass other data that is associated with the same record as the order id, such as order_date, then name that order_date[$id]

 

When processing

<?php
foreach ($_POST['order'] as $id = $x) {

        $order_date = $_POST['order_date'][$id];   // gets associated date field

        // process select orders
}
?>

 

An other way is to switch it round slightly and name the checkbox as name="orders[]" and value="$id" (similar to what you had originally except name end with [] ).

 

Now you can easily pull the orders with selected ids with

 

<?php
$idlist = join (',' , $_POST['orders'] );
$sql = "SELECT * FROM orders WHERE id IN ($idlist)";
?>

Thanks, got it working, I want to take it one step further now.

 

If I go to a different page, where it has another list and tick some more how can I make it remember the ones I ticked on the previous page? So when I compile a new page with only the ones ticked, it shows from both pages and not just one?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.