这里我正在尝试提交一个表单。我想知道的是当用户输入他的名字时,如果该名字已经在数据库中,我怎么能阻止他提交表单并给出错误信息。它应该检查他打字时的名字。
这是我的代码
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>gooooooooo </title>
<link rel="stylesheet" href="style.css" type="text/css"/>
<link rel="stylesheet" type="text/css" href="demo/demo.css" >
<link rel="stylesheet" href="demo/codemirror.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script src="js/additional-methods.min.js"></script>
<!--<script src="script.js"></script>-->
<script type="text/javascript">
function copyText() {
src = document.getElementById("fname");
dest = document.getElementById("sitename");
dest.value = src.value;
}
</script>
</head>
<body>
<div class="wrapper">
<header>
<img src="images/logo.png" width="200" align="center" id="logo" style="text-align: center" />
</header>
<div id="login-form" style="margin-top: 5px !important;">
<form action="" method="post">
<table align="middle" border="0" style="margin: 0 auto; background: rgba(57, 134, 79, 0.64);" >
<h1 align="middle" style="color: #fff">Create Your Web Site</h1></br></br>
<tr>
<td><input type="text" id="Mnumber" name="Mnumber" value="" placeholder="Your Mobile Number" required /></td>
</tr>
<tr>
<td><input type="email" id="email" name="email" value="" placeholder="Your Email" required /></td>
</tr>
<tr>
<td><input type="text" id="fname" name="fname" value="" placeholder="Your First Name" onkeyup="copyText()" required /></td>
</tr>
<tr>
<td><input type="text" id="address" name="address" value="" placeholder="Your Address" required /></td>
</tr>
<tr>
<td><input type="text" id="sitename" name="sitename" value="" placeholder="Your Site name" readonly/></td>
</tr>
<tr>
<td><button type="submit" id="btn-signup" name="btn-signup" style="background: #39864f; border: 1px solid #fff; cursor: hand">Create website</button></td>
</tr>
</table>
</form>
<script>
</script>
</div>
<footer>
<p style="text-align: center;"><a href="#" style="color: #fff">Powered By <img src="images/elogo.png" align="center" id="logo" style="text-align: center; height: 40px; width: 153px;padding-bottom: 10px;" /></a></p>
</footer>
</div>
</body>
</html>
这是我的表格
require __DIR__.'../../vendor/autoload.php';
答案 0 :(得分:0)
您只需要更改条件,因为mysql_num_rows()返回使用您的查询从数据库中提取的行数。 您的查询用于选择包含new_fname的行数。如果该名称的记录可用,则返回1。
所以你需要检查mysql_num_rows($ xxx)&gt; 0,然后回显错误,否则你的工作。
if(mysql_num_rows($xxx) > 0)
{
echo"error";
}
else
{
$query = mysql_query("INSERT INTO gotest(Mnumber,email,fname,address,sitename,reqnum,payment) VALUES('$Mnumber','$email','$fname','$address','$sitename','$reqnum','$payment')");
$r = mysql_insert_id();
header("Location: http://uuuu.yyyyy.net/test/freecreate.php?rid=$r");
}
你可以通过ajax检查如下
您需要附加jquery库以使用jquery函数
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn-signup").attr('disabled', true); // This will disable submit button.
$("#fname").keyup(function(){
var fname = $(this).val();
$.ajax({
url: "check_name.php",
type: "POST",
data: {fname : fname }
success: function(response){
if(response=="ok")
{
$("#btn-signup").attr('disabled', false); // Enable submit button.
}
else
{
alert("Fname is already exist.");
$("#btn-signup").attr('disabled', true);
}
}
});
})
})
</script>
您的用于检查姓名的php脚本是否已存在
check_name.php
<?php
$new_fname= $_POST['fname'];
$xxx = mysql_query("SELECT fname FROM gotest WHERE fname = '$new_fname'")or die(mysql_error());
$yyy = mysql_fetch_row($xxx);
if(mysql_num_rows($xxx) > 0)
{
echo "exist";
}
else
{
echo "ok";
}
?>