am_25 Posted February 12, 2007 Share Posted February 12, 2007 Hi, I'm passing a array varible thru a multi select list box, but unable to get any value from it thru the $_POST, it returns no value at all. Please see the following code. No value is returned for $test=$_POST['test']; <?php include "connect.php"; echo '<pre>' . print_r($_POST,true) . '</pre>'; if (isset($_POST['submit'])) { $test=$_POST['test']; $n=count($test); echo "You selected ".$test." value. <br />"; if ($test){ foreach ($test as $t){echo 'You selected ',$t,'<br />';} } } ?> <html> <body> <form action="multiple.php" method="post"> <?php $sql2="SELECT distinct(designation) from empdet order by designation asc "; $result2=mysql_query($sql2); ?> <SELECT multiple ="multiple" name="test[]"> <?php While ($row=mysql_fetch_assoc($result2)) { $desi=$row['designation']; echo "<OPTION value='$desi'>$desi</OPTION> "; } ?></select>   <input type='submit' name='submit' value='submit'></form> </form></td></tr> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/ Share on other sites More sharing options...
paul2463 Posted February 12, 2007 Share Posted February 12, 2007 try removing the [] from around the name of your SELECT multiple Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-182519 Share on other sites More sharing options...
am_25 Posted February 12, 2007 Author Share Posted February 12, 2007 it works but the problem is, if I select 3 values in the list box, only the last value is stored for e.g, if I have chosed, MGR, QA,SE - only SE value is displayed. I want all the values. Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-182533 Share on other sites More sharing options...
paul2463 Posted February 12, 2007 Share Posted February 12, 2007 try this <?php if (isset($_POST['submit'])) { $test=$_POST['test']; for($i=0;$i<sizeof($_POST["test"]);$i++) { echo "You selected ".$_POST["test"][$i]."<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-182538 Share on other sites More sharing options...
thepip3r Posted February 12, 2007 Share Posted February 12, 2007 http://onlinetools.org/tricks/using_multiple_select.php Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-182544 Share on other sites More sharing options...
am_25 Posted February 12, 2007 Author Share Posted February 12, 2007 Nope it does not work. If the value is Manager, it just returns M. The select name is just test, I didnot name it as an array. If I place a [] with test, then nothing gets stored in the variable. <?php include "connect.php"; echo '<pre>' . print_r($_POST,true) . '</pre>'; if (isset($_POST['submit'])) { $test=$_POST['test']; //$n=count($test); for($i=0;$i<sizeof($_POST["test"]);$i++) { echo "You selected ".$_POST["test"][$i]."<br />"; } //echo "You selected ".$test." value. <br />"; if ($test){ foreach ($test as $t){echo 'You selected ',$t,'<br />';} } } ?> <html> <body> <form action="multiple.php" method="post"> <?php $sql2="SELECT distinct(designation) from empdet order by designation asc "; $result2=mysql_query($sql2); ?> <SELECT multiple ="multiple" name="test[]"> <?php $i=1; While ($row=mysql_fetch_assoc($result2)) { $desi=$row['designation']; echo "<OPTION value='$desi'>$desi</OPTION> "; $i=$i+1; } ?></select>   <input type='submit' name='submit' value='submit'></form> </form></td></tr> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-182547 Share on other sites More sharing options...
am_25 Posted February 12, 2007 Author Share Posted February 12, 2007 Thanks I tried the Online tools example, but it does not work when I retrieve data from the DB. When the OPTION VALUES are given it works, but when it is retrieved from the DB, the array does not pass any value. It is very weird Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-182554 Share on other sites More sharing options...
thepip3r Posted February 12, 2007 Share Posted February 12, 2007 ok then.. try this because this doesn't make sense. I've gone over your code but can't seem to find a problem so here's what i want you to do. Load the page so that your db query runs and builds your select box. Then view source on that page and post the results here. i want to see how the raw HTML output looks. Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-182565 Share on other sites More sharing options...
am_25 Posted February 12, 2007 Author Share Posted February 12, 2007 Thanks a lot Here is the raw HTML code <pre>Array ( [test] => [submit] => submit ) </pre>You selected value. <br /><html> <body> <form action="multiple.php" method="post"> <SELECT multiple ="multiple" name="test[]"> <OPTION value='Manger'>Manger</OPTION> <OPTION value='Mgr'>Mgr</OPTION> <OPTION value='QA'>QA</OPTION> <OPTION value='SE'>SE</OPTION> <OPTION value='SSE'>SSE</OPTION> </select>   <input type='submit' name='submit' value='submit'></form> </form></td></tr> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-182586 Share on other sites More sharing options...
kenrbnsn Posted February 12, 2007 Share Posted February 12, 2007 You have an extraneous "</form></td></tr>" at the end of your source, but that shouldn't make a difference. I took your code and ran it on my server and it works fine. What version of PHP, type of webserver, and browser are you using? Ken Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-182684 Share on other sites More sharing options...
am_25 Posted February 13, 2007 Author Share Posted February 13, 2007 I'm using PHP 5.0, IE 6.0 Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-183262 Share on other sites More sharing options...
13th_Star Posted February 13, 2007 Share Posted February 13, 2007 instead of if ($text) { foreach try if ($test == !"") { foreach Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-183269 Share on other sites More sharing options...
am_25 Posted February 13, 2007 Author Share Posted February 13, 2007 Apache 2.2.4 Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-183270 Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 instead of if ($text) { foreach try if ($test == !"") { foreach That doesn't make sense. If test is going to be an array, check the count(); if(count($test)){ } Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-183273 Share on other sites More sharing options...
13th_Star Posted February 13, 2007 Share Posted February 13, 2007 instead of if ($text) { foreach try if ($test == !"") { foreach That doesn't make sense. If test is going to be an array, check the count(); if(count($test)){ } well everytime I try to use if($blabla) it does fuck all lol Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-183283 Share on other sites More sharing options...
am_25 Posted February 13, 2007 Author Share Posted February 13, 2007 I think it is something to do with PHP 5 Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-183304 Share on other sites More sharing options...
am_25 Posted February 13, 2007 Author Share Posted February 13, 2007 This works <?php echo '<pre>' . print_r($_POST,true) . '</pre>'; $test=$_POST['test']; if ($test){ foreach ($test as $t){echo 'You selected ',$t,'<br />';} } ?> <html> </body> <form action="test.php" method="post"> <select name="test[]" multiple="multiple"> <option value="one">one</option> <option value="two">two</option> <option value="three">three</option> <option value="four">four</option> <option value="five">five</option> </select>   <input type='submit' name='submit' value='submit'> </form> </body> </html> This does not work, what can the error be, except that the values are from the DB. It is not passed in the array varible. But for a variable it works. <?php include "connect.php"; echo '<pre>' . print_r($_POST,true) . '</pre>'; if (isset($_POST['submit'])) { $test=$_POST['test']; $n=count($test); if (count($test)){ echo " you are inside the loop"; } echo "You selected ".$test." value. <br />"; if ($test){ foreach ($test as $t){echo 'You selected ',$t,'<br />';} } } ?> <html> <body> <form action="multiple.php" method="post"> <?php $sql2="SELECT distinct(designation) from empdet order by designation asc "; $result2=mysql_query($sql2);?> <SELECT name="test[]" multiple ="multiple"> <?php While ($row=mysql_fetch_assoc($result2)) { //$desi=$row['designation']; echo "<OPTION value=\"{$row['designation']}\">{$row['designation']}</OPTION>\n"; } ?></select> <input type='submit' name='submit' value='submit'> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-183352 Share on other sites More sharing options...
am_25 Posted February 14, 2007 Author Share Posted February 14, 2007 It worked, the problem was very simple. the include "connect.php"; - line was the problem. Since this line was the first line, all the POST variables were getting lost. I removed this line of code and placed it just before where the SQL was called and viola everything worked. Thanks for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/38119-solved-please-help-unable-to-get-value-in-_post-for-a-array-variable/#findComment-184406 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.