Jump to content

[SOLVED] please help - Unable to get value in &_POST for a array variable


am_25

Recommended Posts

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>
&nbsp
<input type='submit' name='submit' value='submit'></form>
</form></td></tr>
</body>
</html>



Link to comment
Share on other sites

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>

&nbsp

<input type='submit' name='submit' value='submit'></form>

</form></td></tr>

</body>

</html>

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

&nbsp

<input type='submit' name='submit' value='submit'></form>

</form></td></tr>

</body>

</html>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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> 
&nbsp
<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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.