s4salman Posted May 15, 2008 Share Posted May 15, 2008 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 More sharing options...
wrathican Posted May 15, 2008 Share Posted May 15, 2008 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. Link to comment https://forums.phpfreaks.com/topic/105706-populate-drop-down-using-php-mysql/#findComment-541590 Share on other sites More sharing options...
s4salman Posted May 15, 2008 Author Share Posted May 15, 2008 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> Link to comment https://forums.phpfreaks.com/topic/105706-populate-drop-down-using-php-mysql/#findComment-541629 Share on other sites More sharing options...
wrathican Posted May 15, 2008 Share Posted May 15, 2008 is this your full script for add2.php if not we need it so we can see line 5 Link to comment https://forums.phpfreaks.com/topic/105706-populate-drop-down-using-php-mysql/#findComment-541632 Share on other sites More sharing options...
s4salman Posted May 15, 2008 Author Share Posted May 15, 2008 Dear i am weak at echo records in table. Can you please help me in this regard or identify some source where i can learn it. Thanks Link to comment https://forums.phpfreaks.com/topic/105706-populate-drop-down-using-php-mysql/#findComment-541633 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.