JSON数组存储先前的数据结果

时间:2018-10-03 13:06:17

标签: javascript php jquery ajax

所以我的问题是在第一次尝试更新个人资料时,这没关系。它只输出一则消息和一个结果。但是,在第二次尝试时,它将输出两个消息和两个结果。我已经清除了数组,以为它是存储先前结果的数组,但是它什么也没做。

我的js文件中的内容如下:

$('#edit_my_profile').on('submit', function(e){
  e.preventDefault();

  var fname = $('#my_profile_fname').val(),
      mname = $('#my_profile_mname').val(),
      lname = $('#my_profile_lname').val(),
      user_number = $('#my_profile_user_number').val(),
      parent_number = $('#my_profile_parent_number').val(),
      department = $('#my_profile_department').val(),
      course = $('#my_profile_course').val(),
      year_level = $('#my_profile_year').val(),
      section = $('#my_profile_section').val(),
      username = $('#my_profile_username').val(),
      password = $('#my_profile_password').val(),
      alert = $('#my_profile_message_board');

  if(!validate([fname, mname, lname, user_number, parent_number, department, course, year_level, section, username, password])){
    notify('<i class="fas fa-times fa-lg mr-2"></i> <b>Error!</b> Cannot leave empty fields!', 'danger');
  } else if(password == 'password12345ddd'){
    notify('<i class="fas fa-times fa-lg mr-2"></i> <b>Error!</b> Please change the default password!', 'danger');
  } else{
    var modal = $('#modal_confirm_update_profile');

    modal.modal('show');

    $('#btn_update_profile').on('click', function(){
      modal.modal('hide')
      var data = $('#edit_my_profile').serializeArray();
      data[data.length] = {name : "edit_my_profile", value : 1};

      $.ajax({
        url : controllers('ProfilesController'),
        method : 'POST',
        data : data,
        dataType : 'JSON',
        success : function(e){
          // notify(e.message, e.alert);
          // my_profile_information();
          console.log(e)
        }
      })
    })
  }
})

这是我的ProfilesController.php:

if(isset($_POST['edit_my_profile'])){
    $fname = $init->post('my_profile_fname');
    $mname = $init->post('my_profile_mname');
    $lname = $init->post('my_profile_lname');
    $user_number = $init->post('my_profile_user_number');
    $parent_number = $init->post('my_profile_parent_number');
    $section = $init->post('my_profile_section');
    $username = $init->post('my_profile_username');
    $password = encrypt($init->post('my_profile_password'));
    $updated_at = Carbon::parse($user_updated_at);

    $validate = validate([$fname, $mname, $lname, $user_number, $parent_number, $section, $username, $password]);

    $now = Carbon::now()->toDayDateTimeString();
    $length_in_days = $updated_at->diffInMinutes($now);

    if($length_in_days < 7){
        $json['bool'] = false;
        $json['alert'] = 'danger';
        $json['message'] = "<b>Error!</b> Calm down officer! You have recently updated your profile $length_in_days ago!";
    } else{
        if(!$validate){
            $json['bool'] = false;
            $json['alert'] = 'danger';
            $json['message'] = '<b>Error!</b> Cannot leave empty fields!';
        } else{
            $sql = $init->query("UPDATE users SET fname = '$fname', mname = '$mname', lname = '$lname', user_number = '$user_number', parent_number = '$parent_number', section_id = '$section', username = '$username', password = '$password' WHERE student_id = {$_SESSION['student_id']}");

            if($sql){
                $json['bool'] = true;
                $json['alert'] = 'primary';
                $json['message'] = '<i class="fas fa-thumbs-up fa-lg fa-spin"></i> Successfully updated your profile!';
            } else{
                $json['bool'] = false;
                $json['alert'] = 'danger';
                $json['message'] = '<b>Error!</b> Something went wrong!';
            }
        }
    }

    $json['error'] = $init->error();
    echo json_encode($json);
}

第二次尝试的结果是这个(chrome的控制台):

{bool: false, alert: "danger", message: "<b>Error!</b> Calm down officer! You have recently updated your profile 5 ago!", error: ""}
{bool: false, alert: "danger", message: "<b>Error!</b> Calm down officer! You have recently updated your profile 5 ago!", error: ""}
{bool: false, alert: "danger", message: "<b>Error!</b> Calm down officer! You have recently updated your profile 5 ago!", error: ""}

它应该只输出一个。

2 个答案:

答案 0 :(得分:1)

问题来自将click侦听器添加到#btn_update_profile侦听器的submit内部 中。每次提交表单时,都会告知该按钮单击该按钮,以调用新函数,这就是第二个提交两次发送POST请求的原因,以及为什么您在控制台中看到两个日志的原因。

假设#btn_update_profile位于模态内部,请更改代码,以便仅添加一次click侦听器,然后在该侦听器内部检查变量以查看表单是否已提交

答案 1 :(得分:0)

每次调用$('#edit_my_profile').on('submit',...)时,它都会获得JSON调用n次。 像这样从其中取出$('#btn_update_profile').on('click',...)

var modal = $('#modal_confirm_update_profile');
$('#edit_my_profile').on('submit', function(e) {
  e.preventDefault();

  var fname = $('#my_profile_fname').val(),
      mname = $('#my_profile_mname').val(),
      lname = $('#my_profile_lname').val(),
      user_number = $('#my_profile_user_number').val(),
      parent_number = $('#my_profile_parent_number').val(),
      department = $('#my_profile_department').val(),
      course = $('#my_profile_course').val(),
      year_level = $('#my_profile_year').val(),
      section = $('#my_profile_section').val(),
      username = $('#my_profile_username').val(),
      password = $('#my_profile_password').val(),
      alert = $('#my_profile_message_board');

  if(!validate([fname, mname, lname, user_number, parent_number, department, course, year_level, section, username, password])){
    notify('<i class="fas fa-times fa-lg mr-2"></i> <b>Error!</b> Cannot leave empty fields!', 'danger');
  } else if(password == 'password12345ddd'){
    notify('<i class="fas fa-times fa-lg mr-2"></i> <b>Error!</b> Please change the default password!', 'danger');
  } else {
    modal.modal('show');
  }
});
$('#btn_update_profile').on('click', function() {
  modal.modal('hide')
  var data = $('#edit_my_profile').serializeArray();
  data[data.length] = {name : "edit_my_profile", value : 1};
  $.ajax({
    url : controllers('ProfilesController'),
    method : 'POST',
    data : data,
    dataType : 'JSON',
    success : function(e){
      // notify(e.message, e.alert);
      // my_profile_information();
      console.log(e)
    }
  });
});