How can dropdown list be used instead of checkbox?

This code is working for me…Now Based on the selection of this can the information be displayed?..I am on the process of writting the code…Plz do guide if I encounter any problem…Thanks for the support…

For example i have information stored in database about color…Now on selection of colors from dropdownlist how those info will be displayed in another page or form?

 Colors<select name="Colors">
<option  value="red">red</option>
<option  value="green">green</option>
<option  value="yellow">yellow</option>
<option  value="black">black</option>
<option  value="blue">blue</option>
</select>
<input type="submit" value="submit"/>

You mean, how do you pre-select that color when the return to the form? I’m still not following your line of questioning.

ok…To be more precise or clear…I have description about each color that is stored in database…If I want to display that information i.e when I select particular color and submit it how can the information stored in database be displayed?

Oh, you will need to use a SELECT query to retrieve it based on the color selected.

thats true…But do I need to write sql code for each color?
$sql = "SELECT colorinfo FROM colours WHERE Color = ‘black’ ";
this would display the info abt black…what if I needed info about blue?.I should manually go back to code and make the changes…Is there any other way out?

You need to persist the form input, either in $_SESSION or by some other means, and use that in your WHERE clause.

Yes, when you submit the form, take the value of the colour selection from the drop-down list, use that in the query to retrieve the information, and then display it.

Thanks…But that means i have to write separate queries for each color…or change the query each time.

Why?

It wouldn’t be any different than your INSERT statement that takes the value from the form and puts in the INSERT statement to insert into the database. You have the value the user selected, put that in your WHERE clause.

No it doesn’t. As @cpradio said, just take the form variable and use it in your query.

sorry…but could u elabarote more or give an example and exxplain of what ur saying…

thanks…but could you please demonstrate with an example so that i am able to understand it better…Thanks it would be helpful

Very roughly, but based on your own code from further up

if (isset($_POST['Colors'])) { 
  $cl = $_POST['Colors'];
  $query = "Select * from colours where colors = '$cl'";
  if (! $res=mysql_query($query, $con)) {
    die("Error");
  }
  $row = mysql_fetch_assoc($res);
  echo $row['shade'];
}

Check and double-check your column, table and variable names and if this is a new database, think about them - just in typing seven or eight lines there we’ve got “Colors” and “colours” and it just gets confusing.

Also I’m not 100% sure about retrieving the data using the no-longer-supported mysql calls, so there might be some syntax issues there. You need to move to something else, especially if this is new code or you’re learning - it’s a waste of time learning how to use code that is no longer part of the language.

so u mean I should write the code in PDO format?

Would you suggest some sites where I could get the reference about the latest mysql with php?

I’m not sure I’d call something that has been enabled as default since June 2009 “latest”.

Anyway, the online PHP documentation has a lot of example code, and the “User Contributed Notes” often come in handy.

http://php.net/manual/en/book.pdo.php

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.