Jump to content

PHP Multi Selection Lists


mattyt81

Recommended Posts

Hi

 

I have to multi select tables, the table on the left is field with the contents of an array, I then have it so the user can make there selections from the left box and move them into the right. They then hit submit and it gets stored inmysql table as an array. The problem I am  having is keeping the selections in that right box after any sort of page refresh.

Any ideas I have pasted the php and html code below, the add/remove function is controlled by javascript, I won't bother pasting that as i don't think it has anything to do with it.

 

PHP ********

<?php
/ ------- PARSING PERSONAL TRAINER DETAILS VENUE ---------
if ($_POST['parse_var'] == "fromPool"){
    
    $venue = $_POST['venue'];
    if (is_array($_POST['venue'])) {
  $venue = explode("\n", $_POST['venue']);
} else {
  $venue = $_POST['venue'];
}

     // Update the database data now here for all fields posted in the form
     $sqlUpdate = mysql_query("UPDATE ptdata SET venue='$venue' WHERE id='$id' LIMIT 1");
     if ($sqlUpdate){
            $success_msg = '<img src="images/Form/round_success.png" width="20" height="20" alt="Success" />Your venue information has been updated successfully';
     } else {
            $error_msg = '<img src="images/Form/round_error.png" width="20" height="20" alt="Failure" /> ERROR: Problems arose during the information exchange, please try again later</font>';
     }
}
// ------- END PARSING PERSONAL TRAINER DETAILS VENUE ---------
?>
<?php

//////////   Venue Array PHP For Multi Selection Boxes  ////////////

$dbString = ''; // To be pulled from Database

$toPool = explode(',', $dbString); 

$pool = Array('Your Home','My Home','Outside','Private Studio','Your Work','Gym');

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $toPool = (count($_POST['venue']) > 0)? $_POST['venue'] : Array();

    $newDbString = implode(',', $toPool); //  Store in Database;


}

$fromPool = array_diff($pool, $toPool);


?>

html ***********

<form action="ptmemberaccount.php" method="post" enctype="multipart/form-data"onsubmit="multipleSelectOnSubmit()"> 
<table width="500" align="left">
  <tr>
    <td width="500" height="300" align="center">
    <select multiple="multiple" name="fromBox[]" id="fromBox">  


<?php



foreach($fromPool as $itm){

    echo "\t" . '<option value="' . $itm . '">' . $itm . '</option>' . PHP_EOL;

}

?>

</select>
    <select multiple="multiple" name="venue[]" id="toBox"> 


<?php 



foreach($toPool as $itm){
    
        echo "\t" . '<option value="' . $itm . '">' . $itm . '</option>' . PHP_EOL;

}

?>

</select> </td>
  </tr>
  <tr>
    <td align="center" height="35"><script type="text/javascript"> 

createMovableOptions("fromBox","toBox",400,200,'Training Venues','Selected Training Venues');

</script> 

<p>Use the buttons to Add/Remove selections</p></td></td>
  </tr>
  <tr>
    <td align="center" height="100">
    <input type="submit" value="OK"> 
    
  </tr>
</table>
</form> 

 

If you need anymore info let me know, also if anyone knows a better way of achieving this i am open to suggestions.

 

Many Thanks

 

MOD EDIT: code tags added

Link to comment
https://forums.phpfreaks.com/topic/248832-php-multi-selection-lists/
Share on other sites

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.