数组的两个文本框和以html格式插入数据

时间:2015-02-01 09:13:09

标签: php html

我想动态添加和删除文本框,并希望从数据库中的所有文本框插入数据。我的桌子上有两个字段;一个是nome,另一个是nome2。我使用了以下代码:

<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 () {

        $("#textBoxes").append("<input name='nome[]' type='textbox' id='t"+counter+"' >");
        $("#textBoxes").append("<input name='nome2[]' type='textbox' id='f"+counter+"' ><br>");
         ++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'>

       <input name="nome[]" type='textbox' id='t1' >
       <input name="nome2[]" type='textbox' id='f1' ><br>
       </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 = 'root';
                   $dbPassword = '';
                   $dbDatabase = 'test4';
                    $conn=mysqli_connect($dbHost, $dbUsername, $dbPassword,$dbDatabase);

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

    foreach($_POST['nome'],$_POST['nome2'] as $nome,$nome2){
          mysqli_query($conn,"INSERT INTO regist values ('".mysql_real_escape_string($nome)."'),('".mysql_real_escape_string($nome2)."')");
    }


    }
?>
   </body>
</html>    

0 个答案:

没有答案