1

I'm trying to update the select tag option values after submitting the data in the bootstrap modal and that data goes in the select tag. But problem is that we are having to reload the page In order to show those submitted values in the option tag

Select Tag

$user_id = $_SESSION['user_id'];
$select_customer = "select * from customer where user_id='$user_id'";
$select_customer_query = mysqli_query($connection,$select_customer);

          <div class="form-group">
          <select class="form-control form-control-chosen add-customer-tag" id="invoice_customer_name" name="invoice_customer_name">
          <option disabled selected>Customer Name</option>  
          <?php if(mysqli_num_rows($select_customer_query)>0){
          while($customer_result = mysqli_fetch_assoc($select_customer_query)) { ?>
          <option><?php echo $customer_result['customer_name']; ?></option>
          <?php }} ?>
          <option value="add-new-customer">Add New Customer</option>
          </select>
          </div>

Bootstrap Modal

         <div id="add-customer-modal" class="modal fade add-customer-modal" role="dialog">
         <div class="modal-dialog">
         <div class="modal-content">
         <div class="modal-header">
         <h4 class="modal-title">Add New Customer</h4>
         </div>
         <div class="modal-body">
         <div class="form-group">
         <input type="text" class="form-control" id="customer_name_modal" name="customer_name_modal" placeholder="Customer Name"> 
         </div>
         </div>
         <div class="modal-footer">
         <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
         <button type="button" class="btn btn-primary" data-dismiss="modal" id="add-customer-modal-btn">Save</button>
         <div class="error_message"></div>
         </div>
         </div>
         </div>
         </div>

jQuery Code

   $('#add-customer-modal-btn').on('click',function(){
      var customer_name_modal = $('#customer_name_modal').val();
      $.ajax({
          url: 'includes/add-customer-modal.inc.php',
          type: 'POST',
          data: {customer_name_modal:customer_name_modal},
          success: function(add_customer_modal_result){
            $('.error_message').html(add_customer_modal_result);
          } 
      });
   });

add-customer-modal.inc.php File

<?php 

session_start();

include 'db-connection.php';

   

   $user_id = $_SESSION['user_id'];
   $customer_name = mysqli_real_escape_string($connection, $_POST['customer_name_modal']);
   
   if(empty($customer_name)){
       
       echo "<span class='alert alert-danger'>Customer name cannot be blank</span>";
       
   } else {
       
   $insert_customer = "insert into customer(user_id,customer_name) values('$user_id','$customer_name')";

   $inser_customer_query = mysqli_query($connection,$insert_customer) or die(mysqli_error($connection));

   if($inser_customer_query){
       echo "<span class='alert alert-success'>Customer has been added</span>";
   }
   }


?>

I know code is vulnerable to SQL Injection. This is only for testing purposes, so you can ignore it.

Once customer name has been added through bootstrap modal, then it would be inserted in the select tag but it reflects only after reloading the page

Shakti Goyal
  • 55
  • 1
  • 7
  • How about you do that in frontend inside success function of ajax i.e : `$("#invoice_customer_name").append(" – Swati May 11 '21 at 16:17
  • Just tested it out! but still having to reload the page In order to reflect those values in the select tag – Shakti Goyal May 11 '21 at 16:26
  • "*I know code is vulnerable to SQL Injection. This is only for testing purposes, so you can ignore it.*" **NO!** SQL injection is your most important problem. FIX IT. Immediatelly. Don't waste time with other issues, fix the most important one and then come back to this problem. – Dharman May 11 '21 at 16:29
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman May 11 '21 at 16:30

1 Answers1

1

As i said in my comment you can simply insert new options inside your select-box if value is successfully inserted in db .

Here is working code :

$(".add-customer-tag").chosen()
$('#add-customer-modal-btn').on('click', function() {
  var customer_name_modal = $('#customer_name_modal').val();
  /* $.ajax({
       url: 'includes/add-customer-modal.inc.php',
       type: 'POST',
       data: {customer_name_modal:customer_name_modal},
       success: function(add_customer_modal_result=){*/
  //suppose this return
  add_customer_modal_result = "<span class='alert alert-success'>Customer has been added</span>"
  $('.error_message').html(add_customer_modal_result);
  //check if success class is there
  if ($(add_customer_modal_result).hasClass("alert-success")) {
    $("#invoice_customer_name").append("<option>" + customer_name_modal + "</option") //good insert..
    $("#invoice_customer_name").trigger("chosen:updated");//added this line
  }
  /*  } 
      });*/
});

$(".add-customer-tag").on("change", function() {
  if ($(this).val() == "add-new-customer") {
    $("#add-customer-modal").modal("show")
  }
})
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css" integrity="sha512-yVvxUQV0QESBt1SyZbNJMAwyKvFTLMyXSyBHDO4BG5t7k/Lw34tyqlSDlKIrIENIzCl+RVUNjmCPG+V/GMesRw==" crossorigin="anonymous" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js" integrity="sha512-rMGGF4wg1R73ehtnxXBt5mbUfN9JUJwbk21KMlnLZDJh7BkPmeovBuddZCENJddHYYMkCh9hPFnPmS9sspki8g==" crossorigin="anonymous"></script>

<div class="form-group">
  <select class="form-control form-control-chosen add-customer-tag" id="invoice_customer_name" name="invoice_customer_name">
    <option disabled selected>Customer Name</option>
    <option>
      Abc
    </option>

    <option value="add-new-customer">Add New Customer</option>
  </select>
</div>

<div id="add-customer-modal" class="modal fade add-customer-modal" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Add New Customer</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <input type="text" class="form-control" id="customer_name_modal" name="customer_name_modal" placeholder="Customer Name">
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="add-customer-modal-btn">Save</button>
        <div class="error_message"></div>
      </div>
    </div>
  </div>
</div>
Swati
  • 28,069
  • 4
  • 21
  • 41