In Sample.php I have
<html>
<head>
<title>Test</title>
</head>
<script language="javascript">
var count = 1;
function addRow(divId) {
var parentDiv = document.getElementById(divId);
var eleDiv = document.createElement("div");
eleDiv.setAttribute("name", "olddiv");
eleDiv.setAttribute("id", "olddiv");
var eleText = document.createElement("input");
eleText.setAttribute("type", "text");
eleText.setAttribute("name", 'textbox' + count);
eleText.setAttribute("id", "textbox" + count);
var eleBtn = document.createElement("input");
eleBtn.setAttribute("type", "button");
eleBtn.setAttribute("value", "delete");
eleBtn.setAttribute("name", "button");
eleBtn.setAttribute("id", "button");
eleBtn.setAttribute("onclick", "deleteRow('button')");
parentDiv.appendChild(eleDiv);
eleDiv.appendChild(eleText);
eleDiv.appendChild(eleBtn);
var golu=count.toString();
document.getElementById("h").value= golu;
count++;
}
function deleteRow(tableID) {
var div = document.getElementById('olddiv');
if (div) {
div.parentNode.removeChild(div);
}
var golu=count.toString();
document.getElementById("h").value= golu;
}
var golu=count.toString();
document.getElementById("h").value= golu;
</script>
<body>
<form action="Page.php" method="post">
<input type="button" value="Add Row" onclick="addRow('dataTable')" />
<div id="dataTable"><INPUT type="text" name="textbox0" id="textbox0"/></div>
<input type"hidden" id="h" name="h" value="0"/>
<button type="submit" name="submit" id="submit">Submit</button>
</form>
</body>
</html>
In Page.php I have
<?php
include_once('db.php');
$x=$_POST["h"];
$y=intval($x);
$z=0;
while($z<=$y){
$var[z]=$_POST['textbox'][$z];
echo "$var[$z]";
$sql="INSERT into Data values('".$var[z]."');";
$query = mysql_query($sql);
}
?>
My problems:
My aim is to receive all the value of textbox0, textbox1,..etc. by using loop in page.php.
But I am not able to get the desired result. Actually after submitting, Pape.php appears empty. After receiving all, I also want them to store in MySQL Database.
Whenever I delete a textbox the sequence of the textboxes' id doesn't change.
Please tell me what to do.
You can do this following way.
Whenever you create textbox using JavaScript or jQuery, the maintain the count of the text box, suppose you have two textbox by default on the HTML, so store that count into the hidden field like you did that:
Then try this, you are reading the value in wrong way:
Instead of using
$var[z]=$_POST['textbox'][$z];
use$var[z]=$_POST['textbox'.$z];
.I think instead of editing each textbox
id
value, just remove it from HTML and check into the PHP code:Another way, to solve both of your problems :)
test.html:
test.php:
output: