Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I have a form in html which contains a list. I want to retrieve values from database and these values should be displayed in the list..

Can you give me the codes for this.. please
Thank u.
Posted
Comments
Prasad Khandekar 11-Mar-14 9:37am    
Hello,

Please refer to this tutorial. (http://www.phpro.org/tutorials/Dropdown-Select-With-PHP-and-MySQL.html)

Regards,
Member 10626057 11-Mar-14 9:40am    
Here is my code but only the word "Choose" is displayed in the list.. It does not retrieve any item from my database.. please help me

<!--?php ....


$sql="SELECT lecturer_lastname FROM lecturer";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

$name=$row["lecturer_lastname"];

$options.="$name".;
}
??-->
<SELECT NAME=thing>
<OPTION VALUE=0>Choose

</SELECT>
W Balboos, GHB 11-Mar-14 9:44am    
You need to connect to the database before you can do anything with it.
Member 10626057 11-Mar-14 9:45am    
yaa i've already connected to the database..
stil it is displaying only "Choose" in the list.. not the values from my database..

Hello,

Please modify your code as shown below.
PHP
<select name="thing">
    <option value="0">Choose</option>

<?php
$sql = "SELECT lecturer_lastname FROM lecturer";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
    $name = $row["lecturer_lastname"];
    echo  '<option value="' . $name . '"' . '>' . $name . '</option>' . "\n";
}
?> 
</select>

Regards,
 
Share this answer
 
Comments
Member 10626057 11-Mar-14 10:37am    
I'm getting only the word "Choose" in my list.. The item from the database are not displayed..
Prasad Khandekar 12-Mar-14 2:42am    
Have you tried running the query in MySQLWorkbench? Most likely it's not returning the data. i.e. the table in question does not contain any rows.

Regards,
As your listed under the heading 'php', I should remind you that php is a server-side language: it cannot interact with your form at all.

What you could do is:
1 - Reopen the page with the new data loaded on the server, OR,
2 - Use AJAX (requires javaScript) and repopulate the selected area of the screen directly by
replacing the list with a new one with your new values, OR,
3 - Less commonly, getting the values from the sever (AJAX, again) and using the DOM to modify
the list contents precisely how you wish

In either case, you need to use php to access the database (php is typically the target of an AJAX call).
 
Share this answer
 
Comments
Member 10626057 11-Mar-14 9:48am    
am new to php.. javascript
i want a simple code..
I've tried the code below but it is displaying only the word "Choose" in my list

?php //connection

$sql="SELECT lecturer_lastname FROM lecturer";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

$id=$row["lecturer_lastname"];

$options.="$id".;
}
?>
<SELECT NAME=thing>
<OPTION VALUE=0>Choose

</SELECT>
W Balboos, GHB 11-Mar-14 10:32am    
There is a great deal wrong with the above. Your loading data into $option in a while loop, but it's not an array so you keep appending to it, but not as options, just as values.

So far as I can tell from your code, there's no connection being established to a database/sever.

You only see the word 'Choose' because that's the only option in your HTML <select> statement. Also, you didn't close the option tag. Values are surrounded by quotes (single or double).

You'll need to create your <select> box (or, at least the OPTIONS) inside of your php code.

Something roughly like

ECHO "<select name='thing'>";

while(. . . ) {
$id = ...;
ECHO "<options value='$id'>$id</options>";
}
ECHO "</select>";

As for what what you use for value for each option, I've not idea, so I used $id for both.
Member 10626057 11-Mar-14 10:39am    
I've already made the connection to the database..

; } ECHO "</select>"; is being displayed in my form..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900