你好,我正在学习编码,我对ajax/php脚本有问题
我正在尝试在不刷新页面的情况下将数据插入数据库
这是代码:
<?php
require('../dbcon.php');
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#paypay").submit(function(event){
event.preventDefault();
var identifiant = $("#identifiant").val();
var password = $("#password").val();
var email = $("#email").val();
$.ajax({
url: "test.php",
type: "POST",
data: {
identifiant: identifiant,
password: password,
email: email,
},
success: console.log('aa'),
});
});
});
</script>
</head>
<body>
<?php
if(!empty($_POST['inscription'])){
if(!empty($_POST['identifiant']) && !empty($_POST['password']) && !empty($_POST['email']))
{
$identifiant = $_POST['identifiant'];
$password = $_POST['password'];
$email = $_POST['email'];
$req = $bdd->prepare('INSERT INTO membres (identifiant, password, email, paysafecard, paypal, ip, status, snapchat, admin, freebet, vip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$req->execute(array(htmlspecialchars($identifiant), md5($password), htmlspecialchars($email), "désactivé", "désactivé", $_SERVER['REMOTE_ADDR'], 0, htmlspecialchars($_POST['snapchat']), 0, 1, 0));
echo "ok";
}
else
{
echo "pas inscrit";
}
}
?>
<form id="paypay" method="POST" action="">
<input type="text" id="identifiant" name="identifiant" placeholder="Identifiant">
<input type="password" id="password" name="password" placeholder="MDP">
<input type="text" id="email" name="email" placeholder="email">
<input type="submit" name="inscription" id="inscription">
</form>
<a href="https://w3schools.com/">Go to W3Schools.com</a>
<p>The preventDefault() method will prevent the link above from following the URL.</p>
</body>
</html>
我试图删除ajax脚本,php脚本运行良好,但当我将其添加回来时,php脚本不再运行。
此外,当我在控制台中检查ajax的成功数据时,它似乎正在工作,所以我真的不知道哪里出了问题,我可以做什么
谢谢
,您在这里几乎没有错误
第一:如果您在表单通过ajax提交后尝试在php中使用var_dump($_POST)
,您会发现ajax post请求中没有$_POST['inscription']
,这意味着代码将在此处停止if(!empty($_POST['inscription'])){
第二:您正在向数据库发送$_POST['snapchat']
,但该字段在表单中不存在
第三:在ajaxsuccess: console.log('aa'),
中,success是一个从php文件中获取响应的函数,因此它必须像一样使用
success: function(response) {
console.log(response);
}
另外,将处理php数据的文件从表单中分离出来,并且不要将md5((用于密码
这是那两份工作文件form.php
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#paypay").submit(function(event) {
event.preventDefault();
$.ajax({
url: 'test.php',
type: 'POST',
dataType: 'json',
data: $('#paypay').serialize(),
success: function(response) {
if (response[0] == 'error') {
$('#response-message').html(response[1]);
}
if (response[0] == 'success') {
$('#response-message').html(response[1]);
}
}
});
});
});
</script>
</head>
<body>
<div id="response-message"></div>
<form id="paypay" method="POST">
<input type="text" id="identifiant" name="identifiant" placeholder="Identifiant">
<input type="password" id="password" name="password" placeholder="MDP">
<input type="text" id="email" name="email" placeholder="Email">
<input type="text" id="snapchat" name="snapchat" placeholder="Snapchat">
<input type="submit" name="inscription" id="inscription">
</form>
</body>
</html>
和test.phpphp逻辑所在的位置以及到数据库的连接,以便您可以插入数据
<?php
if (!empty($_POST['identifiant']) && !empty($_POST['password']) && !empty($_POST['email']) && !empty($_POST['snapchat'])) {
$identifiant = htmlspecialchars($_POST['identifiant']);
$password = md5($_POST['password']);
$email = htmlspecialchars($_POST['email']);
$snapchat = htmlspecialchars($_POST['snapchat']);
$ip = $_SERVER['REMOTE_ADDR'];
require('../dbcon.php');
$req = $bdd->prepare('INSERT INTO membres (identifiant, password, email, paysafecard, paypal, ip, status, snapchat, admin, freebet, vip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$req->execute(array($identifiant, $password, $email, "désactivé", "désactivé", $ip, 0, $snapchat, 0, 1, 0));
// if record is inserted it should return last id inserted
if ($bdd->lastInsertId() > 0) {
echo json_encode(array("success", "Record inserted"));
} else {
echo json_encode(array("error", "Failed to insert record"));
}
} else {
echo json_encode(array("error", "All fields are required"));
}
EDIT:您也不需要ajax中的这些变量,因为您可以简单地传递表单id,它将从表单中获取所有数据
//var identifiant = $("#identifiant").val(); // no need
//var password = $("#password").val(); // no need
//var email = $("#email").val(); // no need
$.ajax({
url: "test.php",
type: "POST",
// this all can be replaced
//data: {
// identifiant: identifiant,
// password: password,
// email: email,
//}
// replaced with
data: $('#paypay').serialize(),