Dynamic form insert database
<p>My whole signup page code is here.
<a href="https://www.developphp.com/video/PHP/Sign-Up-Form-and-Email-Activation-PHP-MySQL-JavaScript-Programming-Tutorial" rel="nofollow">Link</a></p>

<p>I'm trying to add this dynamic fields to the signup page code.</p>

<pre><code>&lt;script&gt;

 var i = 1;
 function addKid(){
    if (i &lt;= 6){
      i++;
 var div = document.createElement('div');
 div.style.width = "44%";
 div.style.height = "26px";
 div.style.color = "white";
 div.setAttribute('class', 'myclass');
 div.innerHTML = 'Child : &lt;input type="text" name="child_'+i+'" &gt; Ages : &lt;input type="text" name="ages_'+i+'"&gt;&lt;input type="button" id="add_kid()" onClick="addKid()" value="+" /&gt;&lt;input type="button" value="-" onclick="removeKid(this)"&gt;';
 document.getElementById('kids').appendChild(div);
 }
 }
 function removeKid(div) {
     document.getElementById('kids').removeChild( div.parentNode );
 i--; }
</code></pre>

<p></p>

<pre><code>         &lt;div id="kids"&gt;
     Child : &lt;input id="child_1" type="text" onfocus="emptyElement('status')" onkeyup="restrict('mwhen')" maxlength="50"&gt;

      Ages :  &lt;input id="ages_1" type="text" onfocus="emptyElement('status')" onkeyup="restrict('mwhen')" maxlength="10"&gt;&lt;input type="button" id="add_kid()" onClick="addKid()" value="+" /&gt;
       &lt;/div&gt;
</code></pre>

<p>I can get the "child_1 / chi" and "ages_1 / ags" insert into my database. no problem.</p>

<p>When I try to create a extra dynamic field in my signup, "child_2 and ages_2".....
then I can't get that info from "child_2 and ages_2" insert into my database.</p>

<p>Can some one tell me what I'm doing wrong since I can't get that info into my database.? Am I suppose to call "child_2 / chi2" and "ages_2 / ags2" for something els before it will work.?</p>

<pre><code>if(isset($_POST["u"])){
// CONNECT TO THE DATABASE
include_once("php_includes/db_conx.php");
// GATHER THE POSTED DATA INTO LOCAL VARIABLES
$u = preg_replace('#[^a-z0-9]#i', '', $_POST['u']);
$e = mysqli_real_escape_string($db_conx, $_POST['e']);
$p = $_POST['p'];
$g = preg_replace('#[^a-z]#', '', $_POST['g']);
$chi = preg_replace('#[^a-z.,-/ ]#i', '', $_POST['chi']);
$ags = preg_replace('#[^0-9.]#i', '', $_POST['ags']);
$chi2 = preg_replace('#[^a-z.,-/ ]#i', '', $_POST['chi2']);
$ags2 = preg_replace('#[^0-9.]#i', '', $_POST['ags2']);
$c = preg_replace('#[^a-z ]#i', '', $_POST['c']);
// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
$sql = "SELECT id FROM users WHERE username='$u' LIMIT 1";
$query = mysqli_query($db_conx, $sql); 
$u_check = mysqli_num_rows($query);
// -------------------------------------------
$sql = "SELECT id FROM users WHERE email='$e' LIMIT 1";
$query = mysqli_query($db_conx, $sql); 
$e_check = mysqli_num_rows($query);
// FORM DATA ERROR HANDLING
if($u == "" || $e == "" || $p == "" || $g == "" || $chi == "" || $ags == "" || $chi2 == "" || $ags2 == "" || $c == ""){
</code></pre>

<p>..</p>

<pre><code>        $sql = "INSERT INTO users (username, email, password, gender, child_1, ages_1, child_2, ages_2, country, ip, signup, lastlogin, notescheck)       
            VALUES('$u','$e','$p_hash','$g','$chi','$ags','$chi2','$ags2','$c','$ip',now(),now(),now())";
    $query = mysqli_query($db_conx, $sql); 
    $uid = mysqli_insert_id($db_conx);
</code></pre>

<p>..</p>

<pre><code>function restrict(elem){
var tf = _(elem);
var rx = new RegExp;
if(elem == "email"){
    rx = /[' "]/gi;
} else if(elem == "username"){
    rx = /[^a-z0-9]/gi;
} else if(elem == "child_1"){
    rx = /[^a-z.,-/ ]/gi;
} else if(elem == "ages_1"){
    rx = /[^0-9.]/gi;
} else if(elem == "child_2"){
    rx = /[^a-z.,-/ ]/gi;
} else if(elem == "ages_2"){
    rx = /[^0-9.]/gi;
} 
tf.value = tf.value.replace(rx, "");
</code></pre>

<p>}
..</p>

<pre><code>function signup(){
var u = _("username").value;
var e = _("email").value;
var p1 = _("pass1").value;
var p2 = _("pass2").value;
var c = _("country").value;
var chi = _("child_1").value;
var ags = _("ages_1").value;
var chi2 = _("child_2").value;
var ags2 = _("ages_2").value;
var g = _("gender").value;
var status = _("status");
if(u == "" || e == "" || p1 == "" || p2 == "" || c == "" || chi == "" || ags == "" || chi2 == "" || ags2 == "" || g == ""){
</code></pre>

<p>..</p>

<pre><code>        }
    ajax.send("u="+u+"&amp;e="+e+"&amp;p="+p1+"&amp;c="+c+"&amp;chi="+chi+"&amp;ags="+ags+"&amp;chi2="+chi2+"&amp;ags2="+ags2+"&amp;g="+g);
}
</code></pre>

<p>..</p>