Selecting mutiple entires from a dynamic list box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
4wood
Forum Newbie
Posts: 8
Joined: Fri May 12, 2006 10:25 am

Selecting mutiple entires from a dynamic list box

Post by 4wood »

Hey there

We have been racking our brains trying to figure out this one....maybe someone out there can help. We have a three list boxes that are all fed by mysql DB. When a choice in the first drop down list is chosen it populates the second and third list box with related choices.

In the second list box you can only choose one item (that works fine) but in the third list box we want to be able to select multilple items and pass those to the DB.

It kinda works...when you select multiple items (by holding down the Ctrl key) only the last item selected gets submitted to the DB! :?

How can we get all of the selected items to be submitted?

Anyone's help would be greatly appreciated

Thanks
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Easy to figure out. put this code in the page where your form is submitted and you'll see the answer :)

Code: Select all

echo '<HR>GET<PRE>'; print_r($_GET); echo '</PRE>';
echo '<HR>POST<PRE>'; print_r($_POST); echo '</PRE>';
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Selecting mutiple entires from a dynamic list box

Post by RobertGonzalez »

4wood wrote:It kinda works...when you select multiple items (by holding down the Ctrl key) only the last item selected gets submitted to the DB! :?

How can we get all of the selected items to be submitted?
You need to treat the multiple select field as an array field. I think it is something like this...

Code: Select all

<select multiple name="multilist[]" size=3>
<option value="first">First</option>
<option value="next">Next</option>
<option value="other">Other</option>
<option value="another">Another</option>
<option value="last">Last</option>
</select>
Then on the resulting PHP page you would access them as part of the array $_POST['multilist'] (this would be an array).

Code: Select all

<?php
$sel_list = $_POST['multilist'];
if (is_array($sel_list))
{
    foreach($sel_list as $key => $value)
    {
        echo "This multi key is $key and the value is $value...<br />";
    }
}
?>
User avatar
4wood
Forum Newbie
Posts: 8
Joined: Fri May 12, 2006 10:25 am

Post by 4wood »

Thanks - and don't take this the wrong way - we know what we are sending to the DB is the problem

The problem is that we are only sending the last selction of the multiple selections made in the third list box:

Let me try and show you:

list box 1: nrcan

list box 2: shelly

list box 3: USA
UK
CAN


We want all three items selected from the list box to be submitted to the DB - as it is now only CAN would be submitted

Thanks
User avatar
4wood
Forum Newbie
Posts: 8
Joined: Fri May 12, 2006 10:25 am

Post by 4wood »

Thanks Everah but isn't that just hard coding the values in to the page?

We want to be pulling the values from the DB - or am I reading the code you sent wrong?

D
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Everah is spot on.

you need to make your multiple select box an array and treat it as such on your action page.

page 1:

Code: Select all

<select name="mySelect[]" multiple="multiple">
...
</select>
page 2 (action page):

Code: Select all

foreach($_POST['mySelect'] as $val)
    echo $val;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What is the passed $_POST['list_box_3'] value? If it is passing an array (which it should be doing seeing as it is coming from a multiple select list) then you would have to manipulate the array to get all the values from that element of the $_POST array. Can you show your DB update code so we can see what is happening at that point in your app?
User avatar
4wood
Forum Newbie
Posts: 8
Joined: Fri May 12, 2006 10:25 am

Post by 4wood »

Hmmm we will try what you suggested - give me 5 mins - thanks

D
User avatar
4wood
Forum Newbie
Posts: 8
Joined: Fri May 12, 2006 10:25 am

Post by 4wood »

WHOLLY CRAP! we're close!

the script worked - we got it to pull the multiple values from the form - but it still only submits the last value to the mysql DB - AAAHHHH

so close but...... :o
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Post the code you are using to update that multi select value. There may be something in there that we can help with.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

show us your code within the loop...

edit: damn it Everah...you beat me to it
User avatar
4wood
Forum Newbie
Posts: 8
Joined: Fri May 12, 2006 10:25 am

Post by 4wood »

$client_department_result = mysql_query("SELECT DISTINCT department_name FROM client_departments WHERE department_id='$department'") or die(mysql_error());

while($dept_row=mysql_fetch_array($client_department_result))
{
$client_department = $dept_row['department_name'];
break;
}

// Format Recipient(s) section to include multiple selections
$recipients = $_POST['recipients'];
if(is_array($recipients))
{
foreach($recipients as $key => $value)
{
$recipients = "".$value.", ";
}
}

/*
echo "<hr>GET<PRE>"; print_r($_GET); echo "</PRE>";
echo "<hr>POST<PRE>"; print_r($_POST); echo "</PRE>";*/

Insert new rows into the db with the survey form information
$add_survey = "INSERT INTO reports(client_department, request_date, originator, recipients, reason, status, serial_id, comments)
VALUES('$client_department', '$requestDate', '$originators', '$recipients', '$reason', '$status', '$serialID', '$comments')";
mysql_query($add_survey) or die(mysql_error());
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

4wood wrote:

Code: Select all

$client_department_result = mysql_query("SELECT DISTINCT department_name FROM client_departments WHERE department_id='$department'") or die(mysql_error());

	while($dept_row=mysql_fetch_array($client_department_result))
	{
		$client_department = $dept_row['department_name'];
		break;
	}
	
	// Format Recipient(s) section to include multiple selections
	$recipients = $_POST['recipients'];
	if(is_array($recipients))
	{
		foreach($recipients as $key => $value)
		{
			$recipients = "".$value.", ";
		}
	}
	
	/*
	echo "<hr>GET<PRE>"; print_r($_GET); echo "</PRE>";
	echo "<hr>POST<PRE>"; print_r($_POST); echo "</PRE>";*/
	
	 Insert new rows into the db with the survey form information				
	$add_survey = "INSERT INTO reports(client_department, request_date, originator, recipients, reason, status, serial_id, comments) 
				   VALUES('$client_department', '$requestDate', '$originators', '$recipients', '$reason', '$status', '$serialID', '$comments')";
	mysql_query($add_survey) or die(mysql_error());
Fixed this part...

Code: Select all

// Format Recipient(s) section to include multiple selections
	$recipients = $_POST['recipients'];
	if(is_array($recipients))
	{
		foreach($recipients as $key => $value)
		{
			// Concatenate the string instead of overwrite it
			$recipients .= "".$value.", ";
		}
	}
The dot concatenates the string instead of revaluating it for each loop, as it was doing in your code.
User avatar
4wood
Forum Newbie
Posts: 8
Joined: Fri May 12, 2006 10:25 am

Post by 4wood »

:D

I can't believe it - a dot! well I guess that's better then what it could be - thanks!!!!

I feel that I should buy you drink - or something! never been to Fremont CA - been to Burlingame, Redwood a lot though I guess your right across the San Mateo bridge...maybe next time

Again I can't tell you how much this has helped!!

Have a great weekend - we will!!!!

D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad I could help. Yes, we're across the San Mateo bridge, then south a few miles. As for the drink, go get yourself a cold one and think of the things you could do with a '.' over the next few thousand lines of code. :wink:
Post Reply