php - 该代码在某些方面不起作用。ajax和php代码中的问题
问题描述
我希望有人在我的 ajax 代码中帮助我。问题是(1)当我尝试在操作中添加 if(isset) 时,如果没有这个,它不会将数据插入数据库,它工作正常,使用 if(isset) 这是我的需要。(2) 插入数据后,成功弹出窗口不起作用。
索引.php
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>AJAX PRACISE</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- FOR INSERT DATA INTO DATABASE -->
<script type="text/javascript">
$(document).ready(function(){
$('#submit').click(function(event){
event.preventDefault();
$.ajax({
url: "action.php",
method: "post",
data: $('#myform').serialize(),
success: function(){
swal("Good job!", "You clicked the button!", "success");
}
});
});
});
</script>
<!-- FOR INSERT DATA INTO DATABASE -->
</head>
<body>
<form action="action.php" id="myform" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="email" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="password" id="exampleInputPassword1">
</div>
<button type="submit" class="btn btn-primary" name="insbtn" value="Submit" id="submit">Submit</button>
</form>
</body>
</html>
动作.php
$con = mysqli_connect("localhost", "root", "");
mysqli_select_db($con, "ajax");
if (isset($_POST['insbtn'])) {
$email= $_POST['email'];
$password= $_POST['password'];
$sql ="INSERT INTO `user` (`ID`, `email`, `password`) VALUES (NULL, '$email', '$password')";
$query = mysqli_query($con, $sql);
}
?>
我知道这两个问题太基本了,但我是 ajax 新手,这就是我被卡住的原因。谢谢... :)
解决方案
此处$('#myform').serialize()
不包含提交按钮,因此您必须在后端签入if (isset($_POST['email']) && isset($_POST['password']))
同样在插入后,回显某些内容并在 ajax 响应中进行比较。
注意:您的代码对 SQL 注入开放,因此请保护它。
$(document).ready(function(){
$('#submit').click(function(event){
event.preventDefault();
console.log($('#myform').serialize());
$.ajax({
url: "action.php",
method: "post",
data: $('#myform').serialize(),
success: function(resp){
if(resp == 'success'){
swal("Good job!", "You clicked the button!", "success");
} else {
alert('cant insert data');
}
}
});
});
});
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>AJAX PRACISE</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- FOR INSERT DATA INTO DATABASE -->
</head>
<body>
<form action="action.php" id="myform" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="email" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="password" id="exampleInputPassword1">
</div>
<button type="submit" class="btn btn-primary" name="insbtn" value="Submit" id="submit">Submit</button>
</form>
</body>
</html>
你的action.php
$con = mysqli_connect("localhost", "root", "");
mysqli_select_db($con, "ajax");
if (isset($_POST['email']) && isset($_POST['password'])) {
$email= $_POST['email'];
$password= $_POST['password'];
$sql ="INSERT INTO `user` (`ID`, `email`, `password`) VALUES (NULL, '$email', '$password')";
$query = mysqli_query($con, $sql);
// after insert echo something
echo "success";
}
?>
推荐阅读
- c# - 将文件复制到新创建的目录
- maven - 如何处理一个 maven 版本:设置精确版本
- eclipse - 如何在同一行格式化空括号(Eclipse 格式化程序)
- firebase - 来自 Cloud Firestore 的颤振流:如何将数据从快照复制到类并打印
- python - 如何根据值范围将张量流中的整数列转换为字符串
- python - 试图从字典列表中删除重复的字典值
- javascript - 如何从其他文件夹调用axios
- c++ - libpng 警告:使用 libpng-1.4.12 构建但使用 1.6.37 c++ mac 运行的应用程序
- python - 通过 Jenkins 在 Docker Django 应用程序上运行 Selenium 测试
- python - PyQt5如何启动多进程但不是主线程