gosulove gosulove - 29 days ago 7
jQuery Question

Add & Remove <select></select> is not working dynamically for PHP + Jquery

I have a scenario something like below.
enter image description here

When clicking on the "Add More Colour" button the same

<input type="text"> and <select>


will be added below. However, I stuck at this part where the 2nd row or onwards with empty data inside

Add and remove are working fine just that the Select filed is empty.

What I want to achieve is that every new Row is the same. Anyone know how to fix that?

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID

var x = 1; //initlal 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="product_colour[]"/><select name="product_image[]"></select><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>
</head>
<body>

<?php

$pic[]="yellow.jpg";
$pic[]="blue.jpg";
$pic[]="red.jpg";

?>

<form action="jq2.php" method="POST">

<div class="input_fields_wrap">

<button class="add_field_button">Add More Colour</button>
<br>

<div>
<input type="text" name="product_colour[]">
<select name="product_image[]">

<?php
foreach ($pic as $abc){

echo '<option VALUE="'.$abc.'">'.$abc.'</option>';
}
?>
</select>
</div>

</div>
<button>add product</button><br><br>
</form>
</body>
</html>

Answer

please try below code, hope this will help you.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal 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
            var option = $("select").html();
            $(wrapper).append('<div><input type="text" name="product_colour[]"/><select name="product_image[]">'+option+'</select><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>
</head>
<body>

<?php

$pic[]="yellow.jpg";
$pic[]="blue.jpg";
$pic[]="red.jpg";

?>

<form action="jq2.php" method="POST">

<div class="input_fields_wrap">

    <button class="add_field_button">Add More Colour</button>
    <br>

    <div>
    <input type="text" name="product_colour[]">
    <select name="product_image[]">

    <?php
    foreach ($pic as $abc){

        echo '<option VALUE="'.$abc.'">'.$abc.'</option>';
    }
    ?>
    </select>
    </div>

</div>
<button>add product</button><br><br>
</form>
</body>
</html>
Comments