Jump to content

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?

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.