Jquery Timedatepicker won't work properly on dynamic form
<p>I have successfully modified a code that I found here, which creates new textbox fields using javascript.</p>

<p>I am using timepicker with datepicker jquery plugin for a smooth date and time selection. I have a problem. The timepicker works normally after loading the page but after adding additional input fields, the timepicker stops working. </p>

<p>Here's the html code:</p>

<pre><code>&lt;head&gt;
    &lt;link href="css/jquery-ui.css" rel="stylesheet" type="text/css" media="screen" /&gt;       
    &lt;link href="css/timepicker.css" rel="stylesheet" type="text/css" media="screen" /&gt;
    &lt;script type="text/javascript" src="js/jquery.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="js/jquery-ui.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="js/timepicker.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id="obform" name="obform" action="test5.php" method="POST"&gt;
    &lt;div id="fields"&gt;
    &lt;p&gt;&lt;label&gt;Date From:&lt;/label&gt;&lt;input class="timepicker" name="txtdatefrom0" type="text"/&gt;&lt;/p&gt;       
    &lt;p&gt;&lt;label&gt;Date To:&lt;/label&gt;&lt;input class="timepicker" name="txtdateto0" type="text"/&gt;&lt;/p&gt;
    &lt;input type="hidden" name="num" value="1"/&gt;
    &lt;button id="start" type="button" onclick="addField()"&gt;Add&lt;/button&gt;
    &lt;input type="submit" value="Submit"/&gt;
    &lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
</code></pre>

<p>Here is the script</p>

<pre><code>&lt;script&gt;
$(document).ready(function(){
$('.timepicker').datetimepicker(); 
});

$('#start').click(function(){
$(".timepicker").datetimepicker("destroy");
$(".timepicker").datetimepicker();
});
    var num=0;
    function addField(){
    num++; //1
    if(num&gt;4){
        num--;
    }
    makefields();
}
function rmField(){
    num--;
    if(num&lt;0){
        num++;
    }
    makefields();
}

function makefields(){
var fields="";
for(var o=0;o&lt;=num;o++){
fields+="&lt;p&gt;&lt;label&gt;Date From: &lt;/label&gt;&lt;input class=\"timepicker\" name=\"txtdatefrom"+o+"\" type=\"text\"&gt;&lt;/p&gt;";
fields+="&lt;p&gt;&lt;label&gt;Date To:&lt;/label&gt;&lt;input class=\"timepicker\" name=\"txtdateto"+o+"\" type=\"text\"/&gt;&lt;/p&gt;";
}
fields+="&lt;br/&gt;&lt;input type=\"hidden\" name=\"num\" value=\""+o+"\"/&gt;";
if(num!=3){fields+="&lt;button type=\"button\" onclick=\"addField()\"&gt;Add&lt;/button&gt;";}
if(num&gt;0){fields+="&lt;button type=\"button\" onclick=\"rmField()\"&gt;Remove&lt;/button&gt;";}
fields+="&lt;input type=\"submit\" value=\"Submit\"/&gt;&lt;/form&gt;";
document.getElementById("fields").innerHTML=fields;
}
&lt;/script&gt;
</code></pre>

<p>I am also trying to add the new input values in my database, any ideas on how to do that? Sorry, I'm still new in web development. Hoping to become better someday.</p>

<p>EDIT: Here is the php code that I've been modifying for a while now. I want the newly created fields to be inserted in my database smoothly. I set the maximum of fields to be added to 4. I've been trying to insert the fields dynamically by creating a set of fields ready to catch them in my db table(datefrom1,dateto1,datefrom2,dateto2 and so on but i can't get it to work.</p>

<p>Also, is it possible to add a umber of fields in my sql through code? Like if you have 2 duplicates the program will create 2 new fields in a certain db table? I'd prefer that if possible. I've been looking everywhere in the net but couldn't find the solution. 
    

<pre><code>$mysql_host = 'changed'; // put change for confidentiality
$mysql_user = 'changed';
$mysql_pass = 'changed';

$mysql_db = 'changed';

if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass) || !mysql_select_db($mysql_db)) {
    die(mysql_error());
}
$sets=mysql_real_escape_string($_REQUEST["num"]); 
for($loop=0;$loop&lt;$sets;$loop++){
    $df="txtdatefrom".$loop;
    $dt="txtdateto".$loop;
    if(isset($_POST[$df]) &amp;&amp; isset($_POST[$dt])) {
        $datefrom=mysql_real_escape_string($_REQUEST[$df]);
        $dateto=mysql_real_escape_string($_REQUEST[$dt]);
        $query="INSERT INTO testob (datefrom,dateto) VALUES ('$datefrom','$dateto')";
        $query_run=mysql_query($query);
    }
    else {
        echo 'Fill up all fields';
    }
?&gt; 
</code></pre>
