首页 > 解决方案 > Sweetalert 使用 ajax 和 php 提交 inputValue

问题描述

我有一个 sweetalert 模式,允许用户输入一个 pin。我也验证了它,但任何时候我尝试将数据发送到 php 页面。页面dosent获取数据。这是我的代码示例。

索引页面

<script>
  $(document).ready(function(){ 
    swal({
  title: "Setup Transaction pin",
  text: "Enter a memorable 4digit pin to keep your account safe and make sure every transactions comes from you.",
  type: "input",
  showCancelButton: false,
  closeOnConfirm: false,
  animation: "slide-from-top",
  inputType: "number",
  inputPlaceholder: "Enter a pin"
},
function(inputValue){
  if (inputValue === null){
    swal.showInputError("Please enter a pin!");
    return false
  }
  if (inputValue === "") {
    swal.showInputError("Please enter a pin!");
    return false
  }
  
  if (inputValue.length !== 4) {
    swal.showInputError("Pin must be 4digits!");
    return false
  }
  var pin = inputValue.toString();
     $.ajax({
        url: './backend/edit-profile?action=register_pin',
        type: 'POST',
                data: {
                    pin: pin
        },
        dataType: "json",
        cache: false,
        contentType: false,
        processData: false,
        success : function(data){
          if (data.code == "200"){
             swal({
                                            title: data.msg,
                                            //text: data.msg+inputValue" \n",
                                            type: "success",
                                        });
          } else {
            swal({
                                            title: data.msg,
                                            // text: data.msg+" \n",
                                            type: "error",
                                        });
          }
        }
      });
//   swal("Nice!", "You wrote: " + inputValue, "success");
});
  });
</script>

php代码

<?php
if($action == "register_pin"){
   $pin = $_POST['pin']);
   if(empty($pin) || strlen($pin) !== 4){
       $data['msg'] = "Enter a pin $pin";
   }else{
   
        $data['code'] = 200;
        $data['msg'] = "You entered $pin";
    
   }    
}

?>

除了价值,一切都很好。php 代码会回显 inputValue

标签: javascriptphpsweetalert

解决方案


推荐阅读