Jump to content

[SOLVED] Dynamic drop down menu from sql with "add", how?


felipeebs

Recommended Posts

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>";
}


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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