简体   繁体   中英

Dynamically adding a select box with jquery, php and mysql?

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!

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.

您可以在jquery中克隆现有的选择框以在应用程序中重复使用,它将像在一个选择框中一样显示现有选项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM