如何使用动态文本字段在我的数据库中插入多个文本框值

I'm building form with dynamic textboxes, if you click at the button "add", it appears a new textbox, i'm using php and javascript.

But i just can add into my database the value from the first textbox.

How can i insert multiple values into my database?

<html>
   <head>
      <title></title>
   </head>
   <body>
      <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
      <script type="text/javascript"><!--
         $(document).ready(function(){

         var counter = 2;
         $("#add").click(function () {
         if(counter==11){
             alert("Too many boxes");
             return false;
         }   
             $("#textBoxes").append("<div id='d"+counter+"' ><label for='t2'> Textbox "+counter+"</label><input     type='textbox' id='t"+counter+"' > </div>
");
             ++counter;
         });

         $("#remove").click(function () {
         if(counter==1){
             alert("No boxes");
             return false;
         }   
             --counter;
             $("#d"+counter).remove();
         });
         });
         // --></script>
      </head>
      <body>


            <form method="POST">
        <div id='textBoxes'>
            <label for="t1"> Textbox 1</label>
            <input name="nome" type='textbox' id='t1' ></div>
         </div>

         <input type='button' value='add' id='add'>
         <input type='button' value='remove' id='remove'>
         <input type='submit' name="adicionar" value='adicionar'>
         </form>
         <?php
            $dbHost = 'localhost';
                       $dbUsername = 'hr';
                       $dbPassword = '';
                       $dbDatabase = 'r_inserir';

                       $db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
                       mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");

            if(isSet($_POST['adicionar']))
            {

            $nome=mysql_real_escape_string($_POST['nome']);

            $sql=  mysql_query("INSERT INTO registos(nome) values ('".$nome."')");
            }
            ?>
   </body>
</html>
查看全部
doumizhi0809
doumizhi0809
2014/03/19 10:20
  • php
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复