I have successfully modified a code that I found here, which creates new textbox fields using javascript.
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.
Here's the html code:
<head>
<link href="css/jquery-ui.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/timepicker.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/timepicker.js"></script>
</head>
<body>
<form id="obform" name="obform" action="test5.php" method="POST">
<div id="fields">
<p><label>Date From:</label><input class="timepicker" name="txtdatefrom0" type="text"/></p>
<p><label>Date To:</label><input class="timepicker" name="txtdateto0" type="text"/></p>
<input type="hidden" name="num" value="1"/>
<button id="start" type="button" onclick="addField()">Add</button>
<input type="submit" value="Submit"/>
</div>
</form>
</body>
Here is the script
<script>
$(document).ready(function(){
$('.timepicker').datetimepicker();
});
$('#start').click(function(){
$(".timepicker").datetimepicker("destroy");
$(".timepicker").datetimepicker();
});
var num=0;
function addField(){
num++; //1
if(num>4){
num--;
}
makefields();
}
function rmField(){
num--;
if(num<0){
num++;
}
makefields();
}
function makefields(){
var fields="";
for(var o=0;o<=num;o++){
fields+="<p><label>Date From: </label><input class=\"timepicker\" name=\"txtdatefrom"+o+"\" type=\"text\"></p>";
fields+="<p><label>Date To:</label><input class=\"timepicker\" name=\"txtdateto"+o+"\" type=\"text\"/></p>";
}
fields+="<br/><input type=\"hidden\" name=\"num\" value=\""+o+"\"/>";
if(num!=3){fields+="<button type=\"button\" onclick=\"addField()\">Add</button>";}
if(num>0){fields+="<button type=\"button\" onclick=\"rmField()\">Remove</button>";}
fields+="<input type=\"submit\" value=\"Submit\"/></form>";
document.getElementById("fields").innerHTML=fields;
}
</script>
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.
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.
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.
$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<$sets;$loop++){
$df="txtdatefrom".$loop;
$dt="txtdateto".$loop;
if(isset($_POST[$df]) && 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';
}
?>