I created a page that have a Bootstrap Modal button. When user click on this button, a modal window open and display a form to insert data on Mysql table through Ajax and PHP code. What happens is that my Ajax Script not working properly. I tried to find similar questions but i didn't find a resolution:
- My Ajax php code not working correctly
- Why is code in AJAX success call is not working?
- How to insert into mysql table using ajax?
My table has 3 columns:
ID   --> INT(11) AI
name --> VARCHAR(100)
email--> VARCHAR(100)
And below is the Modal code that i'm using to add data through Ajax script:
<button type="button" class="btn btn-block btn-primary" data-toggle="modal" data-target="#dataModal>ADD USER</button>
<!-- Modal -->
<div class="modal fade" id="dataModal" 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" >Add Users</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
            </button>
         </div>
         <div class="modal-body">
            <form id="usersForm" method="post">
               <input type="text" name="name"/>
               <input type="email" name="email"/>
         </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" >ADD USER</button>
             </form>    
         </div>
      </div>
   </div>
</div>
To send data to database through PHP script (insert.php), i'm using this Ajax script code on my project:
<!--AJAX-->   
<script type="text/javascript">
$(document).on('submit','#usersForm',function(e) {
var Name = $("#name").val();
var Email = $("#email").val();
// AJAX code to send data to php file.
    $.ajax({
        type: "POST",
        url: "insert.php",
        data: {Name:name, Email:email},
        dataType: "JSON",
        success: function(data) {
         alert("Data Inserted Successfully.");
        },
        error: function(err) {
        alert(err);
        }
    });
 }
</script>
Below is the insert.php code that i'm using to insert data on Mysql table:
<?php
include('db_connect.php');
$Name = $_POST['name'];
$Email = $_POST['email'];
$stmt = $connection->prepare("INSERT INTO users (name, email) VALUES(:name, :email)");
 $stmt->bindparam(':name', $Name);
 $stmt->bindparam(':email', $Email);
 if($stmt->execute())
 {
  $res="Data Inserted Successfully:";
  echo json_encode($res);
  }
  else {
  $error="Not Inserted,Some Probelm occur.";
  echo json_encode($error);
  }
  ?>
And my PHP database connection script db_connect.php:
<?php
$username = 'root';
$password = '';
$connection = new PDO( 'mysql:host=localhost;dbname=system', $username, $password );
?>
If i put an action on form tag like this:
form id="usersForm" method="post" action="insert.php"
the data is sent to database but if i remove the action="insert.php" nothing happens when user click on submit button. I think is something related with my Ajax script. What could it be?
 
     
    