How can dropdown list be used instead of checkbox?

typ<form action="test.php" method="post" name="form1" />
<input type="checkbox" name="color[]" value="red" /> red 
<input type="checkbox" name="color[]" value="green" /> green   
<input type="checkbox" name="color[]" value="yellow" /> yellow 
<input type="submit" name="submit" value="submit" />
</form>
if (isset($_POST['color'])){
foreach ($_POST['color'] as $value)
{
echo $value;
echo "<br>";
}
}

Use <select> and a list of <option> statements instead. Not really a PHP thing, though, more a HTML question.

<select>
  <option value="volvo" >Volvo</option>
  <option value="saab"  >Saab</option>
  <option value="opel" >Opel</option>
  <option value="audi" >Audi</option>
</select>

I guess this is what u meant…But my point is how can it get displayed on selection…Like it is happening in checkbox code

type<select>
  <option value="volvo" >Volvo</option>
  <option value="saab"  >Saab</option>
  <option value="opel" >Opel</option>
  <option value="audi" >Audi</option>
</select> or paste code here

Yes, that’s what I meant. I’m not sure what you’re asking now.

Ok to be very clear…I want the same thing what is happening with checkbox to be replaced with dropdown list…Can you please run this code…You will get an 3 different checkbox and upon clicking the submit button it goes to the next page…I want the same thing to happen with dropdownlist instead…Is it possible?

<html>
<head></head>
<body>
<form action="test.php" method="post" name="form1" />
<input type="checkbox" name="color[]" value="red" /> red 
<input type="checkbox" name="color[]" value="green" /> green   
<input type="checkbox" name="color[]" value="yellow" /> yellow 
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

test.php

<?php
if (isset($_POST['color'])){
foreach ($_POST['color'] as $value)
{
echo $value;
echo"<br>";
}
}

Yes. Use the HTML code you posted in post #4, and the multiple parameter, and add the same name in the select tag as you used in the checkbox tag. Your PHP code remains the same.

<html>
<head></head>
<body>
<form action="test.php" method="post" name="form1" />
<select>
  <option value="red" name="color[]" >red</option>
  <option value="green" name="color[]"  >green</option>
  <option value="yellow" name="color[]" >yellow</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

I did these changes but its not happening…

Your HTML is incorrect, you do not put a name attribute on the option tag, but on the select tag.

Also you will need to add the multiple attribute to the select tag.

<html>
<head></head>
<body>
<form action="test.php" method="post" name="form1" />
<select name="color[]">
  <option value="red"  >red</option>
  <option value="green"   >green</option>
  <option value="yellow"  >yellow</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

That is closer, you were still missing the multiple attribute

<select name="color[]" multiple="true">

Hi…Thanks a lot its working but instead of displaying the dropdownlist its displaying all options…like scrolling option…is it any way like how could it display a like a typical dropdown list.

Not using just an HTML element. You’d have to use something like select2 to accomplish that

hi I worked with the above code but when i saw it in database instead of the color name…it has stored the word array in it?why ?

None of the code above exhibits database behavior, can you show us that part of the code? Chances are it is simply moving $_POST[‘color’] into the database, which would result in array, as you just converted the HTML element to return an array of values.

Not using just an HTML element. You’d have to use something like select2 to accomplish that
[/quote]

Thanks for telling about select2…but It is used in javascript and i am using php and html.

Right, you would have to adopt your HTML to be HTML and JavaScript is if you wanted that functionality, otherwise, the standard HTML for select using multiple is a box of options.

ok…so If I use javascript which is not specified to me to be used…does it violates the condition that I used use html and php?..

<?php
$con = mysql_connect('dbserver','dbname','password');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db('information');
 if (isset($_POST['color'])){
foreach ($_POST['color'] as $value)
{
echo $value;
echo "<br>";
}
}
 
$sql="INSERT INTO colours (colors,shade)
VALUES
('$_POST[colors]','$_POST[shade]')";
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>

I guess I have messed up everything…If one is working with a particular type the other is not…Really confusing…

Okay, first, you have two references, ‘color’, which is working, and ‘colors’ which likely isn’t defined anywhere.

How do you want to store the colors selected? As a comma-delimited list? If so, you need to use

$colors = "";
 if (isset($_POST['color'])){
    $colors = implode(','$_POST['color']);
}
 
$sql="INSERT INTO colours (colors,shade)
VALUES
('$colors','$_POST[shade]')";
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

If you are supposed to insert each color as its own record, then you need to do something radically different here.

 if (isset($_POST['color'])){
foreach ($_POST['color'] as $value)
{
$sql="INSERT INTO colours (colors,shade)
VALUES
('$value','$_POST[shade]')";
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
 
echo "1 record added";
}
}