php+;ajax插入表单
您好,我正在学习编写代码,我对ajax/php脚本有一个问题 我试图在不刷新页面的情况下在数据库中插入数据 代码如下:php+;ajax插入表单,php,ajax,Php,Ajax,您好,我正在学习编写代码,我对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"></s
<?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>
$(文档).ready(函数(){
$(“#paypay”)。提交(功能(事件){
event.preventDefault();
var identifiant=$(“#identifiant”).val();
var password=$(“#password”).val();
var email=$(“#email”).val();
$.ajax({
url:“test.php”,
类型:“POST”,
数据:{
识别人:识别人,
密码:密码,
电邮:电邮,,
},
成功:console.log('aa'),
});
});
});
preventDefault()方法将阻止上面的链接跟随URL
我尝试删除ajax脚本,php脚本运行良好,但当我重新添加时,php脚本不再工作
另外,当我在控制台中检查来自ajax的成功数据时,它似乎正在工作,所以我不知道到底是什么错了,我能做什么
谢谢你,伙计你这里几乎没有什么错误 首先:如果在表单通过ajax提交后,您尝试在php中使用
var\u dump($\u POST)
,您将看到在ajax POST请求中没有$\u POST['edinting']
,这意味着如果(!empty($\u POST['edinting']){
Second:您正在向数据库发送$\u POST['snapchat']
,但表单中不存在该字段
Third:在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>
success:console.log('aa')
我认为会在匿名(或命名)函数中抛出一个错误~wrap。如果您发布到同一个页面(如图所示)如果不进一步修改代码,就无法使用PHP的任何返回值,从而从任何期望的响应中去除额外的输出。MD5
在存储用户密码时使用不安全-在使用预处理语句时,请使用密码\u散列
和密码\u验证
任何可能修改sql中使用的原始数据的函数,如以后执行的任何检查,都需要遵守相同的数据操作模式(即:不要对输入数据使用htmlspecialchars
)如果您检查浏览器控制台是否有任何错误,并编辑问题以将这些错误包含在支持问题中,这将非常有用谢谢mate提供的好建议我尝试使用php脚本添加另一个page insert.php,因为它位于同一页面上,但仍然相同,没有发生任何问题谢谢mate提供的提示和帮助工作脚本我是新手,它对我帮助很大。@centurionfeatures没有问题,这就是为什么许多开发人员在这里互相帮助的原因
<?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"));
}
//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(),