I have a input box where user specify the number of new DIV elements to be added dynamically,this request is than send to php via ajax where it prepares a html DIV with some inner input tags with each DIV having unique id attribute generated in incremental way using the number provided by user and sends back to JS,now JS appends this new html in existing form.
I have also provided a remove button with every new DIV by which user can remove any dynamically added DIV.
User can keep on adding and deleting elements any no. of times so i dont want to use database for recoding how many elements added or deleted.
Now i need to some solution where i can create and allocate unique ids to div and also keep there record some where so that when form is submitted i can loop through those record and get user submitted data from those DIVs.
1st HTML CODE:
<tr><td>No. Of Hotel</td><td><input type="text" id="noofhotel" name="noofhotel">
<input class="btn green" type="button" value="Submit" onclick="get_multiple_hotel_div()"></td></tr>
2nd JS CODE
function get_multiple_hotel_div(){
var hotel_no = $("#noofhotel").val();
input_form_data = {
hotel_no : hotel_no
}
$.ajax({
type : "POST", //TODO: Must be changed to POST
data : input_form_data,
url: 'index.php?/process_transcation/get_multiple_hotel_div',
beforeSend : function(){
$('#loading_div').html("<img src='public/images/loading_red.gif' style='margin-left:50%; margin-top:5%;'>");
},
success : function(data_response){
$('#loading_div').empty();
data_response = $.parseJSON(data_response);
$('#multi_hotel_table').append(data_response.div_form); } }); }
3rd PHP CODE to generate DIV
function get_multiple_hotel_div($hotel_no){
for($i=1;$i<=$hotel_no;$i++){
$hotelinput_form.="<div class='subtask_input_form_div multi_hotel_div' id='hoteldiv_".$i."'><p class='basic_eq_div_p'>Hotel Entry     <input type='button' id='remove' name='remove' value='Remove' class='btn green' onclick='remove_div(\"hoteldiv_$i\")'></p>
<table>
<tbody>
<tr>
<td style='display:none;'><input type='hidden' id='hotelid_".$i."' name='mhotelno[]'></td>
</tr>
<tr>
<td class='headtd'>Hotel Name</td>
<td class='headtd'><input type='text' id='mhotelname_".$i."' class='mhotel' name='mhotelname_".$i."' style='width:90% !important;' onkeyup='search_dropdown_data(this.value,this.id,\"hotel_table_$i\",\"mhotelid_$i\",\"$supplier_master_table\")'><div class='dropdown_div'><table id='hotel_table_".$i."' class='dropdown_table'></table></div></td>
<input type='hidden' id='mhotelid_".$i."' name='mhotelid_".$i."' class='mhotel'>
<td class='headtd'>Destination </td>
<td class='headtd'><input type='text' id='mdestination_".$i."' class='mdestination' name='mdestination_".$i."' style='width:90% !important;' onkeyup='search_dropdown_data(this.value,this.id,\"rt_to_table_$i\",\"mdestinationid_$i\",\"$destination_master_table\")'><div class='dropdown_div'><table id='rt_to_table_".$i."' class='dropdown_table'></table></div></td>
<input type='hidden' id='mdestinationid_".$i."' name='mdestinationid_".$i."' class='mdestination'>
</tr></tbody>
</table>
</div>";
}// End of for loop
$response_array = array("div_form"=>$hotelinput_form);
return json_encode($response_array);
}//end of function
4th JS CODE to remove div
function remove_div(div_id){
$("#"+div_id).remove();}