javascript - 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
解决方案
推荐阅读
- groovy - 将列表拆分为子列表,类似于整理,但结果的最大大小
- apache - Tomcat 忽略 Apache 反向代理后面的 X-Forwarded-Proto
- sql - 当两个 CTE 上的 Flag=1 时,如何设置 Flag =0?
- android - 使用 Room 将资产中的 JSON 数据存储到数据库中
- c++ - 从斐波那契序列c ++中的数组中查找正确元素时出错
- asp.net-core - 使用 Yarn 安装 Font Awesome 后如何使用
- c++ - 从组件的 std::type_index 获取最多派生类型
- sql-server - T-SQL:必须声明标量变量 - while 循环中的错误
- html - 使用 svg 源强制 img 大小
- c++ - 为什么即使链接了适当的 .lib,这个 C++ 项目也无法构建?