duanlie2709 2018-10-20 05:32
浏览 78
已采纳

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

This is the form through which I want to send data to changePassword.php. but it didn't send data.

<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>

This is the Ajax code through which the data is sent to 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){

          }
     });
});

And this is the changePassword.php page.

 <?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条回答 默认 最新

  • dsfs23434 2018-10-20 05:41
    关注

    You need to stop browser event propagation when button was clicked. The browser was submitting the form to the current page, and starting the AJAX call concurrently, but the page reload is faster and stop the AJAX call.

    To do this, change your jQuery code to this:

    $('#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!');
        }
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)