Jump to content

Populate drop down using php mysql


s4salman

Recommended Posts

Hello

i have form:

 

<form id="FormName" action="added.php" method="post" name="FormName">
<table width="448" border="0" cellspacing="2" cellpadding="0">
<tr><td width = "150"><div align="right"><label for="cat">cat</label></div></td>
<td>
<select id="cat" name="cat">
<option value="funny">funny sms</option>
<option value="cool">cool</option>
<option value="love">Love sms</option>
<option value="cute">cute sms</option>
<option value="flirt">flirt sms</option>
<option value="friendship">friendship sms</option>
<option value="goodluck">goodluck sms</option>
<option value="adult">adult</option>
<option value="jokes">SMS Jokes</option>
</td></tr>
<tr><td width = "150"><div align="right"><label for="sms">sms</label></div></td>
<td><textarea id="sms" name="sms" rows="4" cols="40"></textarea></td></tr><tr><td width="150"></td><td>
<input type="submit" name="submitButtonName" value="Add"></td>
</tr></table></form>

 

But now i want to populate this form drop down list using a mysql table called "cat"

I tried too much but did not got any success.

Please anyone can help.

This cat table has following structure:

 

CREATE TABLE `cat` (
`id` int(6) NOT NULL auto_increment,
`cat` varchar(255) NOT NULL default '',
PRIMARY KEY  (`id`),
UNIQUE KEY `id` (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 

 

 

Please reply i am waiting for a reply

Link to comment
https://forums.phpfreaks.com/topic/105706-populate-drop-down-using-php-mysql/
Share on other sites

what you need to do is query your database and pull the ID and Cat from there.

then use a while loop to loop through the results and display the option eachtime.

example:

<?php

$query = "SELECT * FROM table";
$result = mysql_query($query);

//you need to start the <select> outside of the while
echo '<select id="cat" name="cat">';

while ($row = mysql_fetch_array($result) {
//this is where you echo the cat name and id
$id = $row['id'];
$name = $row['name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo '</select>'

?>

 

when using this in a form (with a submit button) you would need:

<?php

$cat = $_POST['cat'];

?>

 

remember that the $cat variable will be the id of the cat from the table.

I finally correct the script.This may help others as well:

 


<form id="FormName" action="added.php" method="post" name="FormName">
<table width="448" border="0" cellspacing="2" cellpadding="0">


<tr><td width = "150"><div align="right"><label for="cat">cat</label></div></td>
<td>
<?php

include("connect.php");
$query="SELECT cat,id FROM cat";

$result = mysql_query ($query);
echo "<select name=student value=''>Category</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[cat]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
?>

</td></tr>


<tr><td width = "150"><div align="right"><label for="sms">sms</label></div></td>
<td><textarea id="sms" name="sms" rows="4" cols="40"></textarea></td></tr><tr><td width="150"></td><td>
<input type="submit" name="submitButtonName" value="Add"></td>
</tr></table></form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.