Request List Box Values as an array

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

Post Reply
jmoore2141
Forum Newbie
Posts: 3
Joined: Wed May 09, 2007 10:00 am

Request List Box Values as an array

Post by jmoore2141 »

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a page which allows a user to add items to a listbox.  When the form submits I want to capture all values from the listbox on a second page as an array so that I can save them to a table.  I am currently using the request method however I am only capturing one of the values from the list box and cant' seem to find any examples on how to proceed.  Any help will be appreciated.

Thank you,
Josh


The code for the two files is as follows:

file:   test1.php

Code: Select all

<HTML>
<HEAD>
<TITLE>SDLC Input</TITLE>
<script language="JavaScript">

function AddItems()
{     
            var myForm = document.Form1; //Form Name
            var mySel = myForm.sel1;         // Listbox Name    
            var myOption;            
                        
            myOption = document.createElement("Option");                
            myOption.text = document.getElementById("project_number").value;  //Textbox's value
            myOption.value = document.getElementById("project_number").value;  //Textbox's value
            mySel.add(myOption);
}

// begin list clear

function clearlistbox(lb)
{
  for (var i=lb.options.length-1; i>=0; i--)
  {
    lb.options[i] = null;
  }
  lb.selectedIndex = -1;
}




</script>
</HEAD>
<body> 
<FORM name="Form1" METHOD="POST" ACTION="test2.php">
<center><h3>Project Checklist</h3>
<hr>
</center>


<? // Project Number ?>
<tr><td>
<b>Project Number:</b>
</td><td>
<input type="text" size=6 id="project_number"  name="project_number"   value="" >
<align="center" valign="middle">
			<input type="button" value="Add"
			 onclick="AddItems()">
			<input type="button" value="Clear"
			 onclick="clearlistbox(sel1)" />

<select name="sel1" id="sel1" size="5" multiple="multiple" >
</select>
&nbsp;&nbsp;
<a href=javascript:launchwin('proj_numbers.htm','Project_Numbers','height=300,width=600,scrollbars,resizable')>Project Codes</a>
</td></tr>
</table>
<center><INPUT TYPE=SUBMIT VALUE='<<<<<<  SAVE  >>>>>>>'></center>  
</BODY>
</HTML>


file: test2.php

Code: Select all

<?php

$sel1 = trim($_REQUEST['sel1']);

echo $sel1;
			
?>

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do this on the result page and report back:

Code: Select all

<?php
echo '<pre>'; var_dump($_POST); echo '</pre>';
?>
jmoore2141
Forum Newbie
Posts: 3
Joined: Wed May 09, 2007 10:00 am

Still retrieving single value

Post by jmoore2141 »

Thank you for the response. I inserted the requested and and was able to return the last item from the list box.

For instance if I entered values (111, 112) into the listbox on page test1.php and highlighted both values before submitting, the echo on page2.php returns:

array(2) {
["project_number"]=>
string(3) "112"
["sel1"]=>
string(3) "112"
}

Hope this helps.

Thank you,

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

Post by RobertGonzalez »

Change the name of the list box element. You need to pass it as an array, like this:

Code: Select all

<select name="multi_box[]" size="10">
<!-- Options here -->
</select>
I am guessing that you are building the listbox as a single list box. Since the data is sent as an array, but it is being dimensioned as a 1D array, you are only getting the last value as all the earlier values are overwritten for each value passed.
jmoore2141
Forum Newbie
Posts: 3
Joined: Wed May 09, 2007 10:00 am

Resolved

Post by jmoore2141 »

Just discoverd that by adding [] to the name of the element the code you sent began returning all values. Thank you for the help.

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

Post by RobertGonzalez »

You got it bud. Glad I could help.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

I was testing this for myself and have a quick question. I have the following code:

Code: Select all

<?php
if (isset($_POST['submitted'])){
	$ais = $_POST['test'];
	echo '<pre>';
	echo print_r($ais);
	echo '</pre>';
}else{
?>
<html>
<body>
<form action="#" method="post">
<select name="test[]" size="5" multiple="multiple">
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option>
<option value="test4">test4</option>
<option value="test5">test5</option>
</select>
<input type="submit" name="submit" value="submit" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
}
?>
</body>
</html>
At the bottom of the output, there is a '1'. Where does that come from?

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

Post by RobertGonzalez »

Can you show the actual output?
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

go to multi.php and select a couple items from the list box. Click on submit and you'll see a one at the bottom of the array.

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

Post by RobertGonzalez »

Does the same thing on my machine. That is odd, I have never seen that before. var_dump() doesn't do it, but print_r does.

Edit | I think it is because you are echoing a printing function. Remove echo (since print_r outputs anyway) and it goes away.
Post Reply