I write this code to take the tasks and their no from user
<form method="POST" action="add.php">
<tr> <td colspan="4">Tasks</td> </tr>
<tr>
<td colspan="4">
<div id="tasksInput">
Task Name<input type="text" name="mytasks[]"/>
No <input type="text" name="myvol[]"/>
<input type="button" value="add" onClick="add('tasksInput');">
</div>
</td> </tr>
<input type="submit"/> </form>
Java script "tasksInput" function is
<script type="text/javascript">
var count = 1;
var limitt = 100;
function add(divName2){
if (count == limitt) {
alert("You have reached the limit of adding " + count + " inputs");
}
else {
var newdiv2 = document.createElement('div');
newdiv2.innerHTML = "Task " + (count + 1) + " <input type='text' name='mytask[]'>" + "No <input type='text' name='myvol[]'/>";
document.getElementById(divName2).appendChild(newdiv2);
count++;
}
}
</script>
when i click submit, he must store the task in db, lets say i will store A 1 B 2 C 3
in DB he will store only the Task B and C and The no 1 , 2 Could you help me plz
here is my php code
$needs=$_POST['mytask'];
$no=$_POST['myvol'];
$N = count($needs);
if(! empty($_POST['mytask']))
{
for($i=0; $i < $N; $i++)
{
$needname = $needs[$i];
$noneed= $no[$i];
if(! empty($needname ))
{
$query2=mysql_query("INSERT INTO projecttasks VALUES('','','$needname',' $noneed','')");
}
}
}
your form contains this
<input type="text" name="mytasks[]"/>
and via JS you are trying to add this
<input type='text' name='mytask[]'>
names of these fields don't match. Therefore, on server you receive not exactly what you expect
I'm not sure but the issue seems like because of indices. And both JS and PHP starts counting from 0 for the arrays. Maybe this might be the error
Change this:
var count = 1;
var limitt = 100;
to:
var count = 0;
var limitt = 99;