Jeremy
Jeremy

Reputation: 312

Javascript Dynamic HTML Inputs to POST via PHP (Need to rename field's per added input)

I've seen a few questions that were similar but not specific enough. This one - Submit form input fields added with javascript - is the closest comparison, but it is overwhelming for me (not really a javascript guy).

I'm trying to have dynamically added inputs which post independently on the next page via PHP. My code is as follows (which I found off google).

FORM PAGE

<script language="javascript">
fields = 0;
function addInput() {
if (fields != 19) {
document.getElementById('text').innerHTML += "<li>Link Title:<br /><input type='text' name='title' /><br />Link Address:<br /><input type='text' name='name' /><br />Description:<br /><input type='text' name='copy' /></li>";
fields += 1;
} else {
document.getElementById('text').innerHTML += "<br />Only 20 data entries allowed.";
document.form.add.disabled=true;
}
}
</script>

<form name="form" action="form.php" method="post">
<ul class="form">
<li>
Link Title:<br /><input type="text" name="title" /><br />
Link Address:<br /><input type="text" name="name" /><br />
Description:<br /><input type="text" name="copy" />
</li>
<div id="text">

</div>
</ul>
<input type="button" onclick="addInput()" name="add" value="Add input field" /> <input type="submit" value="Submit" />
</form>

POST PAGE - Wasn't sure where to begin since all of the fields are using the name 'title'? - I'll be okay with this page though.

<?php

    echo $_POST['title'];

?>

Upvotes: 0

Views: 1653

Answers (1)

JTeagle
JTeagle

Reputation: 2196

innerHTML += "<li>Link Title:<br /><input type='text' name='title' /><

If you change that last bit to

name='title" + fields + "' /><

(watch where the second apostrophe went) then they gain unique names of title0, title1, title2...

Upvotes: 1

Related Questions