How to create additional forms for users to fill in and write the whole thing into the database?

<script type="text/javascript"> function forma() { var div = document.getElementById('Result'); div.style.display = (div.style.display == 'none') ? '' : 'none'; div.innerHTML = '<?echo '<b>Ф.И.О./Ник:</b> <input type=text name=name size=50 value=""/>';?>'; } </script> echo '<a href onclick="forma(); return false;">Добавить игрока/команду</a>'; echo '<div id="Result" style="display: none"></div>'; 

So only one form is created.

  • Thank you, it works that way. Now the second question: how is the whole thing written in the database? After all, only the contents of the last form are recorded. - vitagame
  • If several fields have the same name key, then pass in an array: <input type = "text" name = "name []" value = "" /> - Deonis
  • One array is not enough. The database is written Array and that's it. - vitagame
  • Can you write all the code? - vitagame
  • You need to know your task. Do you need to create a separate record in the database for each field value? Or all records should be stored in one line? - Deonis


1 answer 1

If pure JS, then you can:

 <script> function append(id) { var node = document.getElementById(id), newNode = document.createElement('div'); newNode.innerHTML = '<input type="text" name="name" value="" />'; node.appendChild(newNode); return newNode; } </script> <div id="test"></div> <a href="" onclick="append('test'); return false;">CLICK</a> 

But jQuery makes these manipulations easier.

UPD

 if(isset($_POST['name'])){ $name = $_POST['name']; $user= $_POST['user']; $query = "INSERT INTO player_comands (id_user, name) VALUES "; for($i = 0; $i < count($name); $i++){ $query .= "('$user[$i]', '$name[$i]'),"; } $query = substr($query, 0, strlen($query)-1); // убираем лишнюю запятую $res = mysql_query($query); } 
  • I add. CREATE TABLE IF NOT EXISTS player_comands ( id int (11) NOT NULL auto_increment, id_user int (11) NOT NULL, id_comands int (11) NOT NULL, name text NOT NULL, PRIMARY KEY ( id )) ENGINE = MyISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1; - vitagame
  • Aha ... You have an auto increment player_comands field. This means that it does not need to be specified in the request, but instead id_user . Corrected in addition. - Deonis
  • I write player names to the player_comands table. Create a command, write its name in the user_comands table. And then we add players to the team. mysql_query ("INSERT INTO user_comands SET id_user = '". $ user [' id ']. "', namecomands = '" .mysql_real_escape_string ($ namecomands). "" "); $ fidd = mysql_insert_id (); mysql_query ("INSERT INTO player_comands SET id_user = '". $ user [' id ']. "', id_comands = '". $ fidd. "', name = '" .mysql_real_escape_string ($ name). "" "); - vitagame
  • How to make the cycle itself? - vitagame pm
  • Oh ... I wrote a loop in addition to you that processes an array from a form field. Do you only have the value name comes from? Then loop and use it, and substitute the other values ​​based on the first query. And I can't get it in - in the first query in the id_user field what are you adding? The same users as the names and then make? - Deonis