gpt4 book ai didi

javascript - 在 PHP 中创建模式警报而不是页面重定向验证

转载 作者:行者123 更新时间:2023-11-29 22:48:53 24 4
gpt4 key购买 nike

我想要进行服务器端验证。有没有办法让我收到类似 this image 的警报每当用户输入数据库中存在的电子邮件等数据时,当我点击提交按钮时就会出现警报。提前致谢。 :D

这是我的 create_acc.html

<form action="create_acc.php" method="POST" id="fieldform">
<dl>
<p><dt>Email Address:</dt>
<dd>
<input type="text" name="e-mail" id="e-mail" class="text" placeholder="Your Email will be your log in" autocomplete="off">
</dd>
</p>

<p><dt>Create Password:</dt>
<dd>
<input type="password" name="password" id="password" class="text" placeholder="Remember your password" autocomplete="off">
</p>
<p>
<p><dt>Your Complete name:</dt>
<dd>
<input type="text" name="name" id="name" class="text" placeholder="Your Complete name" autocomplete="off">
</p>
<p>
<dt>
<input type="submit" value="Submit">
</dt>
</p>
</dl>
</form>

这是我的 create_acc.php

<?php
session_start();
$email = $_POST['e-mail'];
$name = $_POST['name'];
$pass = $_POST['password'];

$hash = hash('sha256',$pass);

function createSalt(){
$text = md5(uniqid(rand(), true));
return substr($text,0,3);
}

$salt = createSalt();
$pass = hash('sha256',$salt.$hash);

$conn = mysqli_connect('localhost','root','','mydb');

$email = mysqli_real_escape_string($conn,$email);

$query = "INSERT INTO customers(Email,Password,Name,salt) VALUES('$email','$pass','$name','$salt')";

mysqli_query($conn,$query);

mysqli_close($conn);

header('Location: index.php');
?>

最佳答案

您可以使用ajax去做这个。当用户提交表单时,您将向您的 php 脚本发送包含表单数据的 ajax 请求,然后脚本将使用您用来决定是否应该显示警报的值进行响应,这是一个使用 jquery 的基本示例:

$(document).ready(function() {
// catch submit events for your form
$('#your-form-id').submit(function() {
// make an ajax request to your validation script
$.ajax{(
url:'create_acc.php',
type:'POST',
data:$(this).serialize(),
dataType:'json',
success:function(response) {
if(!response.success) {
alert('Something went wrong!');
}
}
});
return false;
});
});

然后在你的 php 脚本中返回一个代码,告诉客户端它是如何进行的:

  // create_acc.php
// I assume the form only has one value 'email'
$success = false;
if(isset($_POST['email'])) {
// here you would check if the email already
// exists in the database
$query = "SELECT * FROM <table> WHERE email = ?";
$stmt = $con->prepare($query);
$stmt->bind_param('s', $_POST['email']);
// execute the query and check if a row was returned
}
// if everything went fine you should change success to true

// return json response to client
$response = array('success' => $success);
print json_encode($response);
exit;

关于javascript - 在 PHP 中创建模式警报而不是页面重定向验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28977252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com