当我单击一个按钮时,它没有通过ajax jquery发送数据到changePassword.php

时间:2018-10-20 05:32:50

标签: php jquery ajax

这是我要通过其发送数据到changePassword.php的形式。但它没有发送数据。

<form class="form-horizontal" method="post">
    <div class="form-group">
        <label for="inputName" class="col-sm-2 control-label">Current Password</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="current_password" name="current_password" placeholder="Name">
        </div>
    </div>
    <div class="form-group">
        <label for="inputEmail" class="col-sm-2 control-label">New Password</label>
        <div class="col-sm-10">
            <input type="password" class="form-control" id="new_password" name="new_password" placeholder="Email">
        </div>
    </div>
    <div class="form-group">
        <label for="inputEmail" class="col-sm-2 control-label">Confirm Password</label>
        <div class="col-sm-10">
            <input type="password" class="form-control" id="confirm_password" name="confirm_password" placeholder="Email">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" name="changePassword" id="changePassword"  class="btn btn-primary">Change Password</button><br><br><br><br><br>
        </div>
    </div>
</form>

这是Ajax代码,数据通过该Ajax代码发送到changePassword.php

$("#changePassword").click(function(event) {
     var old_pass = $('#current_password').val();
     var new_pass =  $('#new_password').val();
     var conf_pass = $('#confirm_password').val();

     $.ajax({
          url: "ajax/changePassword.php",
          type: "post",
          data: ({ old_pass: old_pass, new_pass: new_pass, conf_pass: conf_pass}),
          success: function(data){

          }
     });
});

这是changePassword.php页。

 <?php
    session_start();
    include"../include/connection.php";

     if (!isset($_SESSION['FT_id']))
            {

            }

    $single_user=$_SESSION['FT_id'];
    $query="select * from faculty where faculty_phone=$single_user";
    $cm=sqlsrv_query($conn,$query);
    $resultFT = sqlsrv_fetch_array($cm);        

           echo $password = $_POST['old_pass']; exit;    

    ?>

1 个答案:

答案 0 :(得分:0)

您需要在单击按钮时停止浏览器事件的传播。浏览器正在将表单提交到当前页面,并同时启动AJAX调用,但是页面重新加载更快并停止了AJAX调用。

为此,请将您的jQuery代码更改为此:

$('#changePassword').click ( function ( event)
{
  event && event.preventDefault ();

  $.ajax (
  {
    url: 'ajax/changePassword.php',
    type: 'POST',
    data:
    {
      old_pass: $('#current_password').val (),
      new_pass: $('#new_password').val (),
      conf_pass: $('#confirm_password').val ()
    },
    success: function ( data)
    {
      alert ( 'Password changed!');
    }
  });
});