felipeebs Posted August 31, 2007 Share Posted August 31, 2007 Hi, I don't know if this is trhe right place to post but here I go. I need a simple script that puts the sql result into a drop down menu (<select>) with an option that opens a prompt to add new value (<option>) The code will look like: $link = mysql_connect("$dbhost","$dbuser","$dbpass"); // connect db mysql_select_db("$dbname"); // select db $sql = "select column from table order by column ASC"; // I dunno if it's a valid syntax, is just an example $query = mysql_query($sql); I don't know what to do now, maybe mysql_fetch_array($querry) will help. I want something to return this: <select name="select"> <option value="value1">value1 from sql</option> <option value="value2">value2 from sql</option> <option value="etc">etc from sql</option> <option>this one to add new</option> </select> the last one, i need something, maybe a js, to open a prompt to type the new value that will be submited with the form I hope it's sufficientlly clear to understandwhat I want. So sorry about my bad english. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/67437-solved-dynamic-drop-down-menu-from-sql-with-add-how/ Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 this may help Dynamic DropDown PHP/AJAX $NewData = ""; while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $NewData .= "<option value='".$row['ID']."'>".$row['Name']."</option>\n"; } echo '<select name="select">'; echo $NewData; echo '</select>'; Quote Link to comment https://forums.phpfreaks.com/topic/67437-solved-dynamic-drop-down-menu-from-sql-with-add-how/#findComment-338539 Share on other sites More sharing options...
felipeebs Posted August 31, 2007 Author Share Posted August 31, 2007 Thanks, the first step is done and works right Now Just need the second step: an option to prompt user to add another option I think it's it... Quote Link to comment https://forums.phpfreaks.com/topic/67437-solved-dynamic-drop-down-menu-from-sql-with-add-how/#findComment-338546 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 OK some basic html/JS heres an example ValidateForm <script language = "Javascript"> function ValidateForm(){ var emailID=document.frmSample.txtEmail if ((emailID.value==null)||(emailID.value=="")){ alert("Please Enter your Email ID") emailID.focus() return false } return true } </script> <form name="frmSample" method="post" action="#" onSubmit="return ValidateForm()"> <p>Enter an Email Address : <input type="text" name="txtEmail"> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> </form> i think thats what your after! Quote Link to comment https://forums.phpfreaks.com/topic/67437-solved-dynamic-drop-down-menu-from-sql-with-add-how/#findComment-338549 Share on other sites More sharing options...
Psycho Posted August 31, 2007 Share Posted August 31, 2007 I think you will want to use javascript to implement that functionality. This code will create a select list and a text field. The text field will only be enabled if the "other" optioin is selected in the select list. <script type="text/javascript"> function checkForOther(selectValue, otherID) { otherFieldObj = document.getElementById(otherID); if (selectValue=='other') { otherFieldObj.disabled = false; } else { otherFieldObj.value = ''; otherFieldObj.disabled = true; } return; } </script> <?php $query = "SELECT * FROM listvalues"; $result = mysql_query($query); if (mysql_num_rows($result)) { echo "Select an option: "; echo "<select name=\"select1\" onchange=\"checkForOther(this.value,'text1');\">\n"; while ($record = mysql_fetch_assoc($result)) { echo "<option value=\"{$record['value']}\">{$record['label']}</option>\n"; } echo "<option value=\"other\">Other</option>\n"; echo "</select><br>\n"; echo "Other option: <input type=\"text\" name=\"text1\" id=\"text1\" disabled>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67437-solved-dynamic-drop-down-menu-from-sql-with-add-how/#findComment-338557 Share on other sites More sharing options...
felipeebs Posted August 31, 2007 Author Share Posted August 31, 2007 You all was helpfull, thanks. Topic solved! Quote Link to comment https://forums.phpfreaks.com/topic/67437-solved-dynamic-drop-down-menu-from-sql-with-add-how/#findComment-338809 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.