doumizhi0809
2014-03-19 10:20
浏览 55
已采纳

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

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>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dpnfjx755573 2014-03-19 10:25
    已采纳

    Make the name like this in html:

    <input name="nome[]" type='textbox' id='t1' ></div>
    

    then in you javascript:

    $("#textBoxes").append("<div id='d"+counter+"' ><label for='t2'> Textbox "+counter+"</label><input name='nome[]' type='textbox' id='t"+counter+"' > </div>
    ");
    

    these code return the array data into your php file. then insert it you your database.

    if(isset($_POST['adicionar']))
            {
    
            //$nome=mysql_real_escape_string($_POST['nome']);
            foreach($_POST['nome'] as $nome){
                  mysql_query("INSERT INTO registos(nome) values ('".mysql_real_escape_string($nome)."')");
            }
    
            //$sql=  mysql_query("INSERT INTO registos(nome) values ('".$nome."')");
            }
    

    Important:

    This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题