Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Selection List - Show Previously Selected Values

Status
Not open for further replies.

htdenver

Programmer
Mar 24, 2005
10
US
I have a HTML/PHP form with a Selection List field where the user can select a value from the list, fill out other text fields and save the form. The next time the user visits the site and brings up the form, I want to allow the user to edit previously entered and selected data. Now I'm fine with the text fields, but with the Selection List ..... I want the previously selected and saved 'selection value' as the displayed value in the list, but allowing this to be changed and another value to be selected and this saved. The Selection List is saved in an MySQL table and I have no problem displaying the list from this table, when the user first enters the data. Tried using Implode and Explode not sure how to make it work. I have not written any working code as I do not know where to start, but the following example shows what I am after:

1st time the user uses the form.....
Option A
Option B--- user selects from Selection List and saves as part of form
Option C
Option D
Option E

Option A
Option B..... this option is no longer what the user wants
Option C
Option D --- user changes selection from Selection List & saves as part of form
Option E

I can insert the values into the table (code shown below). But how do I allow them to edit the information with previously selected items shown!!!!!

print "Enter name: <input type=text name=id size=20><br>\n";
print "Select Items: <select name=test[] size=5 multiple >
<option value=item1 >item1</option>
<option value=item2 >item2</option>
<option value=item3 >item3</option>
<option value=item4 >item4</option>
<option value=item5 >item5</option>
<option value=item6 >item6</option>
<option value=item7 >item7</option>
</select><br>\n";
print "<br>\n";
print "(Shif+Right Mouse) for multiple selection<br>\n";
print "<br>\n";
print "<input type=submit value=Submit><input type=reset>\n";

id=$_POST['id'];
$test=$_POST['test'];
$db="select";
$link = mysql_connect("localhost","root","");
//$link = mysql_connect("localhost",$_POST['username'],$_POST['password']);
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link) or die("Select Error: ".mysql_error());
}
//sql = "INSERT INTO table_name VALUES ('" . join(",",$_POST["test"]) . "')";
$result=mysql_query("INSERT INTO multiple (id, test) VALUES ('$id','" . join(",",$_POST["test"]) . "')")or die("Insert Error: ".mysql_error());
mysql_close($link);
print "Record added\n";
 
To display the selected Item from the select box, all you have to do is place [red] SELECTED [/red] inside the appropriate choice.

Like so:

Code:
print "Select Items: <select name=test[] size=5 multiple >
          <option value=item1 >item1</option>
          <option value=item2 >item2</option>
          <option value=item3 [red]SELECTED[/red] >item3</option>
          <option value=item4 >item4</option>
          <option value=item5 >item5</option>
          <option value=item6 >item6</option>
          <option value=item7 >item7</option>
        </select><br>\n";
print "<br>\n";

The thing here is that instead of having a hardcoded select box, you'll need to check if the value macthes the selection l have to option and add SELECTED to the option: This means you'll have to dynamically create the select box comparing the value from the db to each value of the select options:

Code:
...
$options.="<option value=item1 ";
if($valuefromDB==item1){$options.="SELECTED";}
$options.=">Item1</select>";

$options.="<option value=item2 ";
if($valuefromDB==item2){$options.="SELECTED";}
$options.=">Item2</select>";
      
so on with every option.   
...

at the end you can print out the entire contents of $options 
to get the select box.

With that done and the form submitted you can the issue an [red]UPDATE[/red] statement to the DB with the new values.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for the help but that is not going to work for me, I DONT THINK? Maybe im missunderstaning. The values of the select box are stored as comma dimlited. For example:

The field stores the values selected as

Item 1, Item 3, Item 6 if that is what was selected. So not one value but a comma dilimited string. Can or how can I search and compare.

Would it be easier to have a key associated with the field?

1 Item 1
2 Item 2
3 Item 3
4 item 4

And search the key values? But i would still end up with a comma delimitd string...
 
adapt vacunita's suggestion

Code:
$csv = "" ; //comma delimited values
if(strpos($csv, item2, 0) !==FALSE){$options.="SELECTED";}
 
Jpadie is right,
You also use [blue]explode()[/blue] to
get each value separately and then compare:

Code:
$csv=""//comma delimited values.
$values=expolode(","$csv);
if(($item1==$values[0])||($item1==$values[1])||($item1==$values[2]))
{ $options.="SELECTED"; }

Its just a matter of doing the comparisons.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top