简体   繁体   中英

How can i insert multiple textboxes value in my database, using dynamic text fields

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>\n");
             ++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>

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>\n");

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.

Firstly you must have dynamic names for the input fields along with ids, secondly loop through all the input values fetched from post and then save it.

eg:

<input type='textbox' id='t"+counter+"' name='name"+counter+"' >

and then use $_POST['name'], $_POST['name1'] and so on to store it in database.

Not the best solution, but you can let the form send the text fields as an array:

<input type="text" name="nome[]" ... />

Then you can access this array in PHP using:

<?php
    $_POST['nome'] //array containing all the values
?>

Finally, you can store this array serialized in the database. Have a look at http://www.php.net/manual/en/function.serialize.php

<?php
//when you store the data
$nome=mysql_real_escape_string( serialize ($_POST['nome']) );
//....

//when you retrieve the data
$nomes = unserialize($row['nome']); //(for example, returns array with all names)
?>

Important: When using serialized content, you might change the MySQL field type to TEXT instead of VARCHAR because it could be a very long string.

Appendix: You should also use mysqli instead of mysql !

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