Michel Xavier
Michel Xavier

Reputation: 145

My Ajax script not hide Bootstrap Modal after submit

I have a page that display 6 Bootstrap Cards in 3 columns. Each Bootstrap card display an image, name, discribe and a footer button. When user click on Bootstrap Card footer button, a Bootstrap Modal open with a Sql row data by ID and into this Bootstrap Modal, has 2 inputs and one textarea form that are described below:

1. <input type="text" name="name" id="name" class="form-control" required />

2. <input type="email" name="email" id="email" class="form-control" required />

3. <textarea class="form-control" type="text" name="message" id="message" rows="3"  
required></textarea>

To open Modal with respect ID, i inserted a PHP code in button card code like below:

<button type="button"  class="btn btn-block btn-primary" data-toggle="modal" 
data-target="#dataModal<?php echo $record['ID']; ?>">View Details</button>

Where <?php echo $record['ID']; ?> is related to SQL row ID.

When user fills form and click on Modal submit button, all data are sent to database, and this is ok, but Bootstrap Modal not hide after Ajax success.

The Ajax script that i use to insert data through php code (insert.php) in database is:

<script type="text/javascript" language="javascript" >

$(document).on('submit', '#contactForm', function(event){
        event.preventDefault();
    $.ajax({
                url:"insert.php",
                method:'POST',
                data:new FormData(this),
                contentType:false,
                processData:false,
                success:function(data){
                    alert("Message sent!");                 
                    $("#contactForm")[0].reset();
                    $('#dataModal').hide();
                    $(".modal-backdrop").remove();

                }
});
});

</script>

I think the problem is related to this line on Ajax success: $('#dataModal').hide(); because the Bootstrap Modal html id attribute is "dataModal<?php echo $record['ID']; ?>" and not only "dataModal".

In this case if i put like this $('#dataModal<?php echo $record['ID']; ?>').hide(); nothing happens.

Below is the complete code:


<div class="container">

<?php
include_once("includes/mysqli_connection.php");
$sql = "SELECT * FROM products ORDER BY RAND() LIMIT 6";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
while( $record = mysqli_fetch_assoc($resultset) ) {
?>  

<div class="card-deck" style="width: 18rem; display:inline-block; margin:15px;">
  <div class="card">
  <?php echo '<img src="upload/'.$record["image"].'" class="img-thumbnail" width="286" height="180" />'; ?>
    <div class="card-body" align="center"><h5 class="card-title"><?php echo $record['ProductName']; ?></h5></div>
    <div class="card-footer">
      <button type="button"  class="btn btn-block btn-primary" data-toggle="modal" data-target="#dataModal<?php echo $record['ID']; ?>">View Details</button>
    </div>
  </div>
</div>

<!-- Modal -->
<div class="modal fade" id="dataModal<?php echo $record['ID']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" >
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">      
        <h5 class="modal-title" >Product: <?php echo $record['productName']; ?></h5> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>

      <div class="modal-body">

<div class="lead">Ask us about this product</div>

<form id="contactForm" method="post">
<div class="form-row">
<div class="col-md-4 mb-3">
<label>Name</label>
     <input type="text" name="name" id="name" class="form-control" placeholder="Your name" required />
</div>
     <div class="col-md-5 mb-3">
     <label>E-mail</label>
       <input type="email" name="email" id="email" class="form-control" placeholder="Your e-mail" required />
</div>

</div>

<div class="form-row">
<div class="col-md-12 mb-3">
<label>Message</label>
<textarea class="form-control" type="text" name="message" id="message" rows="3"  placeholder="Your message" required></textarea>
</div>
</div>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">CLOSE</button>

         <button type="submit" class="btn btn-success" id="submit" >SEND</button>
</form>
      </div>
    </div>
  </div>
</div>
<?php } ?>
</div>

<script type="text/javascript" language="javascript" >

$(document).on('submit', '#contactForm', function(event){
        event.preventDefault();
    $.ajax({
                url:"insert.php",
                method:'POST',
                data:new FormData(this),
                contentType:false,
                processData:false,
                success:function(data){
                    alert("Message sent!");                 
                    $("#contactForm")[0].reset();
                    $('#dataModal').hide();
                    $(".modal-backdrop").remove();

                }
});
});

</script>

In this case what could it be and what i have to do? Thanks all.

Upvotes: 0

Views: 1521

Answers (1)

Anurag Srivastava
Anurag Srivastava

Reputation: 14413

Edit: You could just use $('.modal').hide() if you want to only hide the modal.

You need to set the value of the modal's id inside a script like:

<script type="text/javascript">
    var id = '#dataModal<?php echo $record['ID']; ?>';
</script>

Then you can use:

$(id).hide();

Upvotes: 1

Related Questions