gpt4 book ai didi

php - 使用 jQuery 和 PHP 检查数据库中是否已存在电子邮件地址

转载 作者:可可西里 更新时间:2023-11-01 07:13:42 24 4
gpt4 key购买 nike

我正在尝试验证表中已存在的电子邮件地址,但这不起作用。

这是我的代码:

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$('#Submit').click(function() {
var emailVal = $('#email').val(); // assuming this is a input text field
$.post('checkemail.php', {'email' : emailVal}, function(data) {
alert(data);
});
});
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="view.php">
<p>
<input type="text" name="email" id="email" />
</p>
<p>
<input type="submit" name="Submit" id="Submit" value="Submit" />
</p>
</form>
</body></html>

当提交按钮被点击时,它应该首先使用 jQuery 验证并调用 checkemail.php 文件,如下所示:

<?php
include("../includes/db.php");

$sql = "SELECT email FROM lf_clients WHERE email = " .$_POST['email'];
$select = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($select);

if (mysqli_num_rows > 0) {
echo "The email already exists.";
}
?>

但是,当我单击提交按钮时,它转到 view.php 而不是 checkemail.php。在重定向到 view.php 之前,它应该首先检查电子邮件是否已经存在。如果电子邮件已经存在,则不应重定向到 view.php。

最佳答案

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){ //newly added
$('#Submit').click(function() {alert('in');
var emailVal = $('#email').val(); // assuming this is a input text field
$.post('checkemail.php', {'email' : emailVal}, function(data) {
if(data=='exist') return false;
else $('#form1').submit();
});
});});
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="view.php">
<p>
<input type="text" name="email" id="email" />
</p>
<p>
<input type="button" name="Submit" id="Submit" value="Submit" />
</p>
</form>
</body>
</html>

php代码

<?php
include("../includes/db.php");

$sql = "SELECT email FROM lf_clients WHERE email = " .$_POST['email'];
$select = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($select);

if (mysqli_num_rows > 0) {
echo "exist";
}else echo 'notexist';
?>

关于php - 使用 jQuery 和 PHP 检查数据库中是否已存在电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11663867/

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