Page 1 of 1

Request List Box Values as an array

Posted: Wed May 09, 2007 10:16 am
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]

Posted: Wed May 09, 2007 12:17 pm
by RobertGonzalez
Do this on the result page and report back:

Code: Select all

<?php
echo '<pre>'; var_dump($_POST); echo '</pre>';
?>

Still retrieving single value

Posted: Wed May 09, 2007 12:51 pm
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

Posted: Wed May 09, 2007 12:57 pm
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.

Resolved

Posted: Wed May 09, 2007 1:03 pm
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

Posted: Wed May 09, 2007 1:27 pm
by RobertGonzalez
You got it bud. Glad I could help.

Posted: Wed May 09, 2007 1:56 pm
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

Posted: Wed May 09, 2007 2:25 pm
by RobertGonzalez
Can you show the actual output?

Posted: Wed May 09, 2007 3:10 pm
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

Posted: Wed May 09, 2007 3:29 pm
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.