0

I have tried several ways to get this to work, but never seem to have any success. I want the innerHTML from the below javascript to display x amount of times, based on the value selected in the dropdown box, then to submit all fields to another php which will insert into mysql. When I have tried cloning the section of HTML, it only posts the values in the last clone to the next form. I would like all values from all "cloned" portions to be sent through. Any help would be greatly appreciated!

I have the following HTML:

<form action="?page=book_add" method="post" name="add_booking" id="book" target="_self" enctype="multipart/form-data">

<center><table><tr><td class="form_label">Passengers</td><td valign="middle">
<select id="numpass" name="numpass" class="form_e"/>
<option value="1" selected="selected">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  </select>
</td></tr></table></center>

<div id="repeat"></div>

<table width="100%"><tr><td></td><td style="text-align:right"><input type="submit" value="Add Booking" /></td></tr>
</table>
</form>

Javascript:

$(function(){
    $("#numpass").change(addInput);
});

var counter = parseInt($("#numpass").val());
var limit = 7;
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "<div class='form_head'>Passenger " + counter + " - Details</div>
<br />
<table width='100%' cellpadding='2' cellspacing='4'>

<tr>
<td class='form_label'>First Name:&nbsp;<input type='text' name='cust_fname[]' class='form_f' required='required'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Last Name:&nbsp;<input type='text' name='cust_lname[]' class='form_f' required='required'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Weight (kg):&nbsp;<input type='text' name='cust_kg[]' class='form_a' required='required'/></td>
</tr>

<tr>
<td class='form_label'>Phone:&nbsp;<input type='text' name='cust_phone[]' class='form_g' required='required'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email:&nbsp;<input type='text' name='cust_email[]' class='form_d' /></td>
</tr>

<tr>
<td>
<table width='100%' cellpadding='2' cellspacing='4'>
<tr><td class='form_label'>Address:</td></tr>
<tr>
<td class='form_label'>Number:&nbsp;<input type='text' name='cust_addr_num[]' class='form_a' />&nbsp;&nbsp;&nbsp;Street:&nbsp;<input type='text' name='cust_addr_street[]' class='form_b' />&nbsp;&nbsp;&nbsp;Suburb/Town:&nbsp;<input type='text' name='cust_addr_sub[]' class='form_f' /></td></tr>
<tr><td class='form_label'>State:&nbsp;<input type='text' name='cust_addr_state[]' class='form_b' />&nbsp;&nbsp;&nbsp;Postcode:&nbsp;<input type='text' name='cust_addr_post[]' class='form_a' />&nbsp;&nbsp;&nbsp;Country:&nbsp;<select class='form_c' name='cust_addr_country[]'>
<option value=''>--- Select Country ---</option>
<option value='Australia'>Australia</option></select></td></tr></table>
</td></tr></table>
<br />";
            $(newdiv).appendTo("#repeat");
          /*document.getElementById(divName).appendChild(newdiv);
          counter++;*/
     }
}

JS Fiddle

James Ham
  • 29
  • 4

1 Answers1

0

I believe you have to add the current counter value to the names of the form tags (so it will be cust_phone[1], cust_phone[2] etc).

Form input field names containing square brackets like field[index]

Community
  • 1
  • 1
shaish
  • 1,479
  • 1
  • 12
  • 23