don
don

Reputation: 1

dynamic form javascript, php, mysql

I already have javascript code to add multiple text fields: What I want to know is how many fields were added and store them in mysql db

EX: 10 fields were added $_GET['1'], $_GET['2'] ............. $_GET['10'] how can I know that specific no of fields submitted to next page in php?

<html>
<head>
<script type="text/javascript">
var intTextBox=0;

//FUNCTION TO ADD TEXT BOX ELEMENT
function addElement()
{
intTextBox = intTextBox + 1;
var contentID = document.getElementById('content');
var newTBDiv = document.createElement('div');
newTBDiv.setAttribute('id','strText'+intTextBox);
newTBDiv.innerHTML = "Text "+intTextBox+": <input type='text' id='" + intTextBox + "' name='" + intTextBox + "'/>";
contentID.appendChild(newTBDiv);
}

//FUNCTION TO REMOVE TEXT BOX ELEMENT
function removeElement()
{
if(intTextBox != 0)
{
var contentID = document.getElementById('content');
contentID.removeChild(document.getElementById('strText'+intTextBox));
intTextBox = intTextBox-1;
}
}
</script>
</head>
<body>
<form action="testjs.php">
<div id="content"></div>
<p><input type="button" onclick="addElement()" value="ADD" > <input type="button" onclick="removeElement()" value="REMOVE" ></p>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>

Upvotes: 0

Views: 364

Answers (2)

Dave
Dave

Reputation: 3658

Increment a hidden form field. Or, loop thru the GET/POST data in your PHP script and count the number of occurrences.

Upvotes: 0

ilSavo
ilSavo

Reputation: 854

In the addElement() function you could increment of 1 an hidden field that will be passed to your action page.
Of course, in your removeElemt() that value have to be decreaded by 1.

Upvotes: 1

Related Questions