hi,
when the user selects more than one from a list box,i want to store all the values in the db table.
i want the user to select only five,if more than five are selected it should display message
thanks

Recommended Answers

All 4 Replies

<?php
   for ($i=0; $i < count($_POST['mylist']);$i++)
{
echo "hi".$i;

     echo $_POST['mylist'][$i];
echo "<br>";
}

?>
<html>
<head></head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="mylist[]" size="5" multiple>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
</select>
<input type="submit" name="submit" value="submit">
</form>

</body>
</html>
Member Avatar for amigura

if you have 5 colunms in db setup then forget for loop just input results as $mylist[0], $mylist[1], etc
if you have one field use implode(',',$mylist)

<?php

$mylist=$_POST['mylist'];

if(count($mylist)>0 and count($mylist)<=5)
{echo 'do db thing here'; print_r($mylist);}
else
{echo 'u must select 1, but no more than 5';}

?>
<html>
<head></head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="mylist[]" size="5" multiple>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
</select>
<input type="submit" name="submit" value="submit">
</form>

</body>
</html>

hmm..i want the user to select only 5 ie i want the allow only 5 and not more than 5

Member Avatar for amigura

hi,
when the user selects more than one from a list box,i want to store all the values in the db table.
i want the user to select only five,if more than five are selected it should display message
thanks

The current code does db insert on a 1 - 5 selection.

so i understand bettter.
so the max is 5. is there a minimum?
or are you saying the user has to select 5 minimum and 5 max.

if so change

<?php

$mylist=$_POST['mylist'];

if(count($mylist)==5)
{echo 'do db thing here'; print_r($mylist);}
else
{echo 'u must select  5';}

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.