Page reloading with e.preventDefault + unable to appen more than once
Hello,
I am struggling with 2 problems, the first one is that I can only run the "appen" function once, and then even if I keep clicking on the function, no more than 1 click of data is added(at least the external file is pulled correctly).
My second problem is that when I try to delete the appended data, the page delete the 2 fields from the external file but refresh the page while doing it
Any idea why I am experiencing both issues please?
Here is my code:
- <div class="col-md-2"></div>
- <div class="col-md-8">
- <form action="" method="post" class="form">
- <div class="form-group">
- <label for="variantgroupname">Variant Group Name (ie: Size)</label>
- <input type="text" class="form-control" placeholder="Variant Group name" name="variant_group_name">
- </div>
- <br> <br> <br>
- <div class="form-group">
- <label for="subvariantname">Sub Variant Name (ie: Small,Medium,Large)</label>
- <input type="text" class="form-control" placeholder="Sub Variant Name" name="sub_variant_name[]">
- </div>
- <div class="form-group">
- <label for="linktotoppinglist">Link Topping List</label>
- <select name="toppinglist[]" class="form-control">
- <?php
- $stmt = $conn->prepare("SELECT id,value FROM vat_rates ORDER BY id");
- $stmt->bind_result($id,$value);
- $stmt->execute();
- $stmt->store_result();
- if($stmt->num_rows() > 0){
- while ($stmt->fetch()) {
- $s = "";
- if(isset($_POST['selectvat']) && ($_POST['selectvat'] == $id))
- {
- $s = " selected";
- }
- echo "<option value='".$id."'".$s.">".$value."</option>";
- }
- }
- $stmt->free_result();
- $stmt->close();
- ?>
- </select>
- </div>
- <div class="input_fields_wrap">
- <div class="load_fields"></div>
- <button class="btn btn-success" id="add_field_button">Add More Fields</button>
- <button class="btn btn-danger remove_field" id="#remove_field">Remove Field</button>
- </div>
- </form>
- </div>
- <div class="col-md-2"></div>
- <script>
- $(document).ready(function() {
- var max_fields = 15; //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
- $('.load_fields').load('index.php?page=admin_add_variant_group_append.php');
- }
- });
- $(wrapper).on("click","#remove_field", function(e){
- $(this).parent('div').remove(); x--;
- e.preventDefault();
- })
- });
- </script>
external file:
- <div class="form-group">
- <label for="subvariantname">Sub Variant Name (ie: Small,Medium,Large)</label>
- <input type="text" class="form-control" placeholder="Sub Variant Name" name="sub_variant_name[]">
- </div>
- <div class="form-group">
- <label for="linktotoppinglist">Link Topping List</label>
- <select name="toppinglist[]" class="form-control">
- <?php
- $stmt = $conn->prepare("SELECT vat_id,vat_value FROM vat_rates ORDER BY vat_id");
- $stmt->bind_result($vat_id,$vat_value);
- $stmt->execute();
- $stmt->store_result();
- if($stmt->num_rows() > 0){
- while ($stmt->fetch()) {
- $s = "";
- if(isset($_POST['selectvat']) && ($_POST['selectvat'] == $vat_id))
- {
- $s = " selected";
- }
- echo "<option value='".$vat_id."'".$s.">".$vat_value."</option>";
- }
- }
- $stmt->free_result();
- $stmt->close();
- ?>
- </select>
- </div>
Thank you.