简体   繁体   中英

Add input field button for drop down box with data from the database

So i have this 'add input field button' code

<script>
$(document).ready(function() {
    var max_fields      = 25; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initial text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><label for="NoTelefon[]">No.Telefon</label><input type="text" name="NoTelefon[]" id="NoTelefon[]" class="required input_field"><a href="#"class="remove_field">Batal</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>

This code will add input field and also can remove it. But now I want to add a drop down box which contains data from the database. I've used this code to create the drop down box

    <label for="KodDaerah">Daerah</label>
                    <?php
                    include('dbase.php');
$sql    = "SELECT KodDaerah, NamaDaerah FROM koddaerah";
                    $result = mysql_query($sql);
                    echo "<select name='KodDaerah' id='KodDaerah' class='input_field' required /><option></option>";
                    while($kod = mysql_fetch_array($result)){
                    echo "<option value=".$kod['KodDaerah'].">" .$kod['NamaDaerah']."</OPTION>";
                    }
                    echo "</select>";
                    ?>  
                    <div class="cleaner_h10"></div> 

Now, I've combined this code with the code above but the add field button would not work, anyone can help?

This is how i combined the two code, just insert the code for drop down box into the arapper.append

<script>
$(document).ready(function() {
    var max_fields      = 25; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initial text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><label for="NoTelefon[]">No.Telefon</label><input type="text" name="NoTelefon[]" id="NoTelefon[]" class="required input_field"> <?php $sql  = "SELECT KodLokasi, NamaLokasi FROM kodlokasi";$result = mysql_query($sql);echo "<select name='KodLokasi[]' id='KodLokasi[]' class='input_field' required /><option></option>";while($kod = mysql_fetch_array($result)){echo "<option value=".$kod['KodLokasi'].">" .$kod['NamaLokasi']."</OPTION>";}echo "</select>";?><a href="#"class="remove_field">Batal</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>

EDIT-------

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
$(document).ready(function() {
    var max_fields      = 25; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initial text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div> <input type="text" name="NoTelefon[]" id="NoTelefon[]" class="required input_field">'+
                                '<?php
                                $sql  = "SELECT KodLokasi, NamaLokasi FROM kodlokasi";
                                $result = mysql_query($sql);
                                echo "<select name=\'KodLokasi[]\' id=\'KodLokasi[]\' class=\'input_field\' required /><option></option>";
                                while($kod = mysql_fetch_array($result)){
                                echo "<option value=".$kod['KodLokasi'].">" .$kod['NamaLokasi']. "</OPTION>";
                                }
                                echo "</select>";
                                ?>'+

                                '<a href="#" class="remove_field">Batal</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>

I believe the main problem you are experiencing is related to PHP placing non-escaped single quotation marks in your JavaScript string (which is delimited with single quotes).

Note: the methods you are using for database queries in PHP are generally considered deprecated, but I left them in here in case your version of PHP only supports functional queries.

Here is an example of how to write the click() event append function:

$(wrapper).append('<div><label for="NoTelefon[]">No.Telefon</label>'+
    '<input type="text" name="NoTelefon[]" id="NoTelefon[]" class="required input_field">'+
    '<?php
        $sql  = "SELECT KodLokasi, NamaLokasi FROM kodlokasi";
        $result = mysql_query($sql);
        echo "<select name=\'KodLokasi[]\' id=\'KodLokasi[]\' class=\'input_field\' required /><option></option>";
        while($kod = mysql_fetch_array($result)){
            echo "<option value=\'".$kod['KodLokasi']."\'>" .$kod['NamaLokasi']. "</OPTION>";
        }
        echo "</select>";
    ?>'+
    '<a href="#" class="remove_field">Batal</a></div>'); //add input box

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM