我试图验证一个电子邮件地址,如果它已经存在于表中,但这不起作用.
这是我的代码:
<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>
Run Code Online (Sandbox Code Playgroud)
单击"提交"按钮时,它应首先使用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.";
}
?>
Run Code Online (Sandbox Code Playgroud)
但是,当我单击提交按钮时,它将转到view.php而不是checkemail.php.在重定向到view.php之前,它应首先检查电子邮件是否已存在.如果电子邮件已存在,则不应重定向到view.php.
nav*_*een 11
<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>
Run Code Online (Sandbox Code Playgroud)
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';
?>
Run Code Online (Sandbox Code Playgroud)