Sunday, 15 April 2012

javascript - add and remove input field function -



javascript - add and remove input field function -

i have problem deleting function, code wrong or something? add together function working fine though..........

plus how insert info added input field database?

<script> $(document).ready(function() { var max_fields = 25; var wrapper = $(".input_fields_wrap"); var add_button = $(".add_field_button"); var x = 1; //initial text box count $(add_button).click(function(e){ e.preventdefault(); if(x < max_fields){ //max input box allowed x++; //text box increment $(wrapper).append('<div><label for="no_telefon">no.telefon: </label><input type="text" name="no_telefon[]" id="no_telefon[]" class="required input_field"><label for="lokasi[]">lokasi: </label><input type="text" name="lokasi[]" id="lokasi[]" class="required input_field"><a href="#" class="remove_field">remove</a></div>'); //add input box } }); $(wrapper).on("click",".remove_field", function(e){ //user click on remove text e.preventdefault(); $(this).parent('div').remove(); x--; }) }); </script>

<div class="input_fields_wrap"> <h3 class="add_field_button"><a href="#">add more fields</a></h3> <label for="no_telefon">no.telefon:</label> <input type="text" id="no_telefon" name="no_telefon" class="required input_field" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')" required/> <label for="lokasi">lokasi:</label> <input type="text" id="lokasi" name="lokasi" class="required input_field" required/> </div> </fieldset>

the sql.......

<?php require("dbase.php"); if ($_post) { $id_akaun = isset($_post['id_akaun']) ? $_post['id_akaun'] : ''; $daerah = isset($_post['daerah']) ? $_post['daerah'] : ''; $kategori_akaun = isset($_post['kategori_akaun']) ? $_post['kategori_akaun'] : ''; $bahagian = isset($_post['bahagian']) ? $_post['bahagian'] : ''; $jenis = isset($_post['jenis']) ? $_post['jenis'] : ''; $no_telefon = isset($_post['no_telefon']) ? $_post['no_telefon'] : ''; $lokasi = isset($_post['lokasi']) ? $_post['lokasi'] : ''; $id = isset($_post['id']) ? $_post['id'] : ''; $sql = mysql_query("insert maklumat_akaun values ('', '$id_akaun' , '$daerah' , '$kategori_akaun' , '$bahagian' )"); $sql = mysql_query("insert detail_akaun values ('', '$jenis' , '$no_telefon' , '$lokasi', '".mysql_insert_id()."' )"); echo "<script type='text/javascript'> alert('akaun berjaya didaftarkan')</script> "; echo "<script type='text/javascript'>window.location='lamanutama.php'</script>"; } ?>

the parent of clicked button not div expession $(this).parent('div').remove(); nothing. utilize closest method instead:

$('fieldset').on("click", ".remove_field", function (e) { //user click on remove text e.preventdefault(); $(this).closest('div').remove(); x--; }); demo: http://jsfiddle.net/11jdyrdf/

javascript php html

No comments:

Post a Comment