0

I have this PHP Code which populates a select menu from a MySQL Database...

<select name="input" id="input">
<?php
$sql="SELECT * from table ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
    echo '<option value="'.$result["db_field"].'">'.$result["db_field"].'</option>';
}
?>
</select>

which works perfectly fine but i need to somehow get it into a javascript function.

I have the javascript code that when you click a button it adds more text boxes and another select menu but it does not populate the data from the database in any new (added on) select menus

6
  • Maybe you can create an Array and use it in JavaScript. Commented Mar 20, 2013 at 23:25
  • i think thats what i need but im not sure how, any chance you could give me a big hand on this please Commented Mar 20, 2013 at 23:26
  • This post can give you an idea, hope that help you! =) stackoverflow.com/questions/9665372/… Commented Mar 20, 2013 at 23:28
  • What is the code that adds the other select menu? Commented Mar 20, 2013 at 23:32
  • hmm - not too sure if thats what i need :( any other ideas? Commented Mar 20, 2013 at 23:33

2 Answers 2

0

You can probably convert the options into JSON using json_encode (I am not a PHP programmer and dont know exact semantics of using it)

In PHP do something like:

echo '<script>var optionsJSON = '.json_encode(mysql_fetch_array($rs)).'</script>'

In javascriptn do something like (I am using jquery):

var select = $('select.classOfThisSelect');
var options = JSON.parse(optionsJSON);
for(var i = 0; i < options.length; i++)
  $('option').attr({value: options[i]}).append(options[i]).appendTo(select);

optionsJSON will be the JSON string which will be globally available You can freely use it in your Javascript function

Note: You may need to surround the json_encode with quotes

Sign up to request clarification or add additional context in comments.

Comments

0

Start by converting the string of values into an array, so you have something like:

var values = ['value0','value1','value2'];

Then you can convert them to options and add them to a select element like:

function addOptions(select, values) {
  for (var i=0, iLen=values.length; i<iLen; i++) {
    select.appendChild(new Option(values[i],values[i]));
  }
}

And call it like:

addOptions(document.getElementById('input'), values);

after the select is added to the page.

Incidentally, you don't need to add both an id and name to form controls. You must have a name for them to be submitted, the ID is unnecessary. If you get a reference to the form you can access controls as named properties of the form, so you might reference the select using:

document.forms[0]['input'];

or

document.forms[0].input;

and so on. Note that "input" isn't a good choice of control name.

3 Comments

aha okay - how would i get the values from the database into the "var values" ?
i cannot work out how to get the mysql values into the values var in javascript?
Are you getting them from an XMLHttpRequest? If so, you can have them returned as JSON and convert that to an array, or just as a delimited string and use split on the delimiter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.