0

Ok I have looked at a few questions on here concerning this topic. But I can not seem to find exactly what I am looking for. I need some help!! I need to dynamically create a select box in a form using jquery, php and mysql. Here is how I have the original select box now without the need to add a new one:

<select name="material" id="selectList">
  <?php
  $get_material = mysql_query("SELECT DISTINCT Familia, anchocm FROM MYS")
     or die(mysql_error());

  while ($info = mysql_fetch_array($get_material)) {
     echo "<option value=\"".$info['anchocm']."\">".$info['Familia']."</option>\n  "; 
  }

   ?>
  </select>

It gets the information from a mysql database and populates the fields. Now I need to add a button next to this select box adding another select box identical to this one. So that they may choose one option in each select box. I have no idea how to go about doing this. If anyone has a better suggestion to use I am open. I just need to get this done before tomorrow morning. Thank you so much in advance!

3
  • 2
    It is not entirely clear what you are trying to accomplish here. Is there any reason you cant just create them both when the page loads and keep one hidden until it is needed? Do you need to change the contents of the second box based on the selection in the first? Commented Jan 18, 2012 at 5:17
  • 1
    I'm sorry but the question is a bit vague. Agree with Justin. Commented Jan 18, 2012 at 5:20
  • Hello thank you for your response. The reason I have this is because the client wants it this way. The select boxes will be identical just with different names for each generated box. He has different types of car parts and he wants people to select one option from the select box and then if they need an additional car part to dynamically add a second drop down box with the same options. I would prefer not to do it this way but I can't convince my client otherwise. So hope this cleared it up. Thanks! Commented Jan 18, 2012 at 5:34

2 Answers 2

3

Say you have a button next to your select box:

<input type="submit" value="Copy" id="copy" />

In jQuery:

$("#copy").click(function(e) {
   $("#foon").clone().removeAttr('id').insertAfter(this);
   e.preventDefault();
});

You should also change the name of the select to "material[]" so you can post an arbitrary number as an array.

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

Comments

0

You can clone the existing select box in jquery for reuse that in your application.It will show you the existing options as you have in one select box.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.