使用Javascript和Php与MySql数据库的注册表
我的网站注册有问题 如果我填写用户名字段,并将其他字段留空,则不会显示错误警报,它只会将用户名保存到数据库中。应该有验证,比如检查年龄是否在18岁以下,但我不知道错误在哪里 register.php(此处的javascript代码和php代码):使用Javascript和Php与MySql数据库的注册表,javascript,php,database,mysqli,Javascript,Php,Database,Mysqli,我的网站注册有问题 如果我填写用户名字段,并将其他字段留空,则不会显示错误警报,它只会将用户名保存到数据库中。应该有验证,比如检查年龄是否在18岁以下,但我不知道错误在哪里 register.php(此处的javascript代码和php代码): 在下面注册 函数getText() { 布尔子=真; var name=document.getElementById(“t1”).value; 如果(name.length50) { sub=假; 警报(“无效用户名,必须为8-10个字符!”)
在下面注册
函数getText()
{
布尔子=真;
var name=document.getElementById(“t1”).value;
如果(name.length<8 | | name.length>50)
{
sub=假;
警报(“无效用户名,必须为8-10个字符!”)
document.getElementById(“示例”).innerHTML=“*”;
}
其他的
{
document.getElementById(“示例”).innerHTML=name;
}
var pass=document.getElementById(“t2”).value;
如果(通过长度<8 | |通过长度>50)
{
sub=假;
警报(“无效密码,必须为8-10个字符!”)
document.getElementById(“sample2”).innerHTML=“*”;
}
其他的
{
sub=真;
}
var rpass=document.getElementById(“t3”).value;
如果(rpass!=通过)
{
sub=假;
警报(“密码不匹配!”)
document.getElementById(“sample3”).innerHTML=“*”;
}
其他的
{
sub=真;
}
var fname=document.getElementById(“t4”).value;
如果(fname.length15)
{
sub=假;
警报(“无效的名字,必须是5-15个字符!”)
}
其他的
{
document.getElementById(“sample11”).innerHTML=fname;
}
var lname=document.getElementById(“t5”).value;
如果(名称长度15)
{
sub=假;
警报(“无效的姓氏,必须为5-15个字符!”)
}
其他的
{
document.getElementById(“sample12”).innerHTML=lname;
}
int age=parseInt(document.getElementById(“t2”).value);
if(年龄<18岁)
{
sub=假;
警告(“不允许未成年人”)
}
其他的
{
document.getElementById(“sample4”).innerHTML=age;
}
var email=document.getElementById(“t7”).value;
如果(email.length<8 | | email.length>50)
{
sub=假;
警报(“无效电子邮件,必须为8-50个字符!”)
document.getElementById(“sample2”).innerHTML=“*”;
}
其他的
{
sub=真;
}
var lotnum=document.getElementById(“t8a”).value;
if(lotnum.length10)
{
sub=假;
警报(“位置无效”)
}
其他的
{
document.getElementById(“sample13”).innerHTML=lotnum;
}
var sub=document.getElementById(“t8b”).值;
如果(子长度10)
{
sub=假;
警报(“位置无效”)
}
其他的
{
document.getElementById(“sample14”).innerHTML=sub;
}
var city=document.getElementById(“t8c”).value;
if(城市长度10)
{
sub=假;
警报(“位置无效”)
}
其他的
{
document.getElementById(“sample15”).innerHTML=city;
}
var country=document.getElementById(“t9”).value;
如果(国家/地区长度30)
{
sub=假;
警报(“位置无效”)
}
其他的
{
document.getElementById(“sample16”).innerHTML=country;
}
int area=parseInt(document.getElementById(“t10a”).value);
如果(面积长度8)
{
sub=假;
警报(“无效区号,必须为3-8位!”)
}
其他的
{
document.getElementById(“sample16”).innerHTML=area;
}
int telnum=parseInt(document.getElementById(“t10b”).value);
如果(telnum.length12)
{
sub=假;
警报(“无效号码!”)
}
其他的
{
document.getElementById(“sample16”).innerHTML=telnum;
}
if(sub==false)
{
返回false;
}
}
用户名:
密码:
重新键入密码:
名字:
姓氏:
年龄:
电邮:
地址:
地段/区块#
Brgy/Subd
城市/直辖市
国家
<html>
<center><h1>Register Below</h1></center>
<body>
<br/>
<script>
function getText()
{
boolean sub = true;
var name = document.getElementById("t1").value;
if(name.length < 8 || name.length > 50)
{
sub=false;
alert("Invalid Username, must be 8 - 10 characters!")
document.getElementById("sample").innerHTML="*";
}
else
{
document.getElementById("sample").innerHTML = name;
}
var pass = document.getElementById("t2").value;
if(pass.length < 8 || pass.length > 50)
{
sub=false;
alert("Invalid Password, must be 8 - 10 characters!")
document.getElementById("sample2").innerHTML="*";
}
else
{
sub=true;
}
var rpass = document.getElementById("t3").value;
if(rpass!=pass)
{
sub=false;
alert("Password dont match!")
document.getElementById("sample3").innerHTML="*";
}
else
{
sub=true;
}
var fname = document.getElementById("t4").value;
if(fname.length<5||fname.length>15)
{
sub=false;
alert("Invalid firstname, must be 5 - 15 characters!")
}
else
{
document.getElementById("sample11").innerHTML = fname;
}
var lname = document.getElementById("t5").value;
if(lname.length<5||lname.length>15)
{
sub=false;
alert("Invalid lastname, must be 5 - 15 characters!")
}
else
{
document.getElementById("sample12").innerHTML = lname;
}
int age = parseInt(document.getElementById("t2").value);
if(age < 18)
{
sub=false;
alert("Minors not allowed.")
}
else
{
document.getElementById("sample4").innerHTML = age;
}
var email = document.getElementById("t7").value;
if(email.length < 8 || email.length > 50)
{
sub=false;
alert("Invalid Email, must be 8 - 50 characters!")
document.getElementById("sample2").innerHTML="*";
}
else
{
sub=true;
}
var lotnum = document.getElementById("t8a").value;
if(lotnum.length<3||lotnum.length>10)
{
sub=false;
alert("Location Invalid.")
}
else
{
document.getElementById("sample13").innerHTML = lotnum;
}
var sub = document.getElementById("t8b").value;
if(sub.length<5||sub.length>10)
{
sub=false;
alert("Location Invalid.")
}
else
{
document.getElementById("sample14").innerHTML = sub;
}
var city = document.getElementById("t8c").value;
if(city.length<5||city.length>10)
{
sub=false;
alert("Location Invalid.")
}
else
{
document.getElementById("sample15").innerHTML = city;
}
var country = document.getElementById("t9").value;
if(country.length<3||country.length>30)
{
sub=false;
alert("Location Invalid.")
}
else
{
document.getElementById("sample16").innerHTML = country;
}
int area = parseInt(document.getElementById("t10a").value);
if(area.length<3||area.length>8)
{
sub=false;
alert("Invalid Area Code, must be 3 - 8 digits!")
}
else
{
document.getElementById("sample16").innerHTML = area;
}
int telnum = parseInt(document.getElementById("t10b").value);
if(telnum.length<10||telnum.length>12)
{
sub=false;
alert("Invalid Number!")
}
else
{
document.getElementById("sample16").innerHTML = telnum;
}
if(sub==false)
{
return false;
}
}
</script>
<center>
<form method = "post" action = "insert.php" onSubmit="return getText()">
<table>
<tr>
<td>Username:</td>
<td colspan="3"><input name="t1" type="text" id="t1"><span id="sample">
</span><br></td>
</tr>
<tr>
<td>Password:</td>
<td colspan="3"><input name="t2" type="password" id="t2"><span id="sample2">
</span><br></td></td>
</tr>
<tr>
<td>Re-type Password:</td>
<td colspan="3"><input name="t3" type="password" id="t3"><span id="sample3">
</span><br></td></td>
</tr>
<tr>
<td>Firstname:</td>
<td colspan="3"><input name="t4" type="text" id="t4"><br></td>
</tr>
<tr>
<td>Lastname:</td>
<td colspan="3"><input name="t5" type="text" id="t5"><br></td>
</tr>
<tr>
<td>Age:</td>
<td colspan="3"><input name="t6" type="text" id="t6"><br></td>
</tr>
<tr>
<td>Email:</td>
<td colspan="3"><input name="t7" type="email" id="t7"><br></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="t8a" id="t8a"><br></td>
<td><input type="text" name="t8b" id="t8b"><br></td>
<td><input type="text" name="t8c" id="t8c"><br></td>
</tr>
<tr>
<td></td>
<td>Lot/Block#</td>
<td>Brgy/Subd</td>
<td>City/Municipality</td>
</tr>
<tr>
<td>Country:</td>
<td colspan="3"><input name="t9" type="text" id="t9"><br></td>
</tr>
<tr>
<td>Contact#:</td>
<td><input name="t10a" type="text" id="t10a"><br></td>
<td><input name="t10b" type="text" id="t10b"><br></td>
</tr>
<tr>
<td></td>
<td>Area Code</td>
<td>Number<td>
</tr>
</table>
<br><br>
<button type="submit" onClick="getText()">Submit</button><br><br>
<p id="log">
</p>
</form>
</center>
</body>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$db = mysql_connect("localhost","root","");
if($db){
mysql_select_db("user1",$db);
$username = $_POST['t1'];
$password = $_POST['t3'];
$firstname = $_POST['t4'];
$lastname = $_POST['t5'];
$age = $_POST['t6'];
$email = $_POST['t7'];
$lot = $_POST['t8a'];
$sub = $_POST['t8b'];
$city = $_POST['t8c'];
$country = $_POST['t9'];
$area = $_POST['t10a'];
$num = $_POST['t10b'];
if(mysql_query("insert into regform(username,password,firstname,lastname,age,email,lot,sub,city,country,area,num) values('$username','$password','$firstname','$lastname','$age','$email','$lot','$sub','$city','$country','$area','$num')"))
{
echo "Register Successful. Click <a href='index.html'> Here </a> to return";
}
else
{
echo "Registration Failed".mysql_error();
}
}
else{
echo "Cannot Connect in Database!";
}
?>
<html>
<center><h1>Register Below</h1></center>
<body>
<br/>
<script>
function getText()
{
var sub = true ;
var name = document.getElementById("t1").value;
if(name.length < 8 || name.length > 50)
{
sub=false;
alert("Invalid Username, must be 8 - 10 characters!");
document.getElementById("sample").innerHTML="*";
}
else
{
document.getElementById("sample").innerHTML = name;
}
var pass = document.getElementById("t2").value;
if(pass.length < 8 || pass.length > 50)
{
sub=false;
alert("Invalid Password, must be 8 - 10 characters!");
document.getElementById("sample2").innerHTML="*";
}
else
{
sub=true;
}
var rpass = document.getElementById("t3").value;
if(rpass !== pass)
{
sub=false;
alert("Password dont match!");
document.getElementById("sample3").innerHTML="*";
}
else
{
sub=true;
}
var fname = document.getElementById("t4").value;
if(fname.length<5||fname.length>15)
{
sub=false;
alert("Invalid firstname, must be 5 - 15 characters!");
}
else
{
document.getElementById("sample11").innerHTML = fname;
}
var lname = document.getElementById("t5").value;
if(lname.length<5||lname.length>15)
{
sub=false;
alert("Invalid lastname, must be 5 - 15 characters!");
}
else
{
document.getElementById("sample12").innerHTML = lname;
}
var age = parseInt(document.getElementById("t2").value);
if(age < 18)
{
sub=false;
alert("Minors not allowed.");
}
else
{
document.getElementById("sample4").innerHTML = age;
}
var email = document.getElementById("t7").value;
if(email.length < 8 || email.length > 50)
{
sub=false;
alert("Invalid Email, must be 8 - 50 characters!");
document.getElementById("sample2").innerHTML="*";
}
else
{
sub=true;
}
var lotnum = document.getElementById("t8a").value;
if(lotnum.length<3||lotnum.length>10)
{
sub=false;
alert("Location Invalid.");
}
else
{
document.getElementById("sample13").innerHTML = lotnum;
}
var sub = document.getElementById("t8b").value;
if(sub.length<5||sub.length>10)
{
sub=false;
alert("Location Invalid.");
}
else
{
document.getElementById("sample14").innerHTML = sub;
}
var city = document.getElementById("t8c").value;
if(city.length<5||city.length>10)
{
sub=false;
alert("Location Invalid.");
}
else
{
document.getElementById("sample15").innerHTML = city;
}
var country = document.getElementById("t9").value;
if(country.length<3||country.length>30)
{
sub=false;
alert("Location Invalid.");
}
else
{
document.getElementById("sample16").innerHTML = country;
}
var area = parseInt(document.getElementById("t10a").value);
if(area.length<3||area.length>8)
{
sub=false;
alert("Invalid Area Code, must be 3 - 8 digits!")
}
else
{
document.getElementById("sample16").innerHTML = area;
}
var telnum = parseInt(document.getElementById("t10b").value);
if(telnum.length<10||telnum.length>12)
{
sub=false;
alert("Invalid Number!");
}
else
{
document.getElementById("sample16").innerHTML = telnum;
}
if(sub === false)
{
return false;
}
}
</script>
<center>
<form method = "post" action = "insert.php">
<table>
<tr>
<td>Username:</td>
<td colspan="3"><input name="t1" type="text" id="t1"><span id="sample">
</span><br></td>
</tr>
<tr>
<td>Password:</td>
<td colspan="3"><input name="t2" type="password" id="t2"><span id="sample2">
</span><br></td></td>
</tr>
<tr>
<td>Re-type Password:</td>
<td colspan="3"><input name="t3" type="password" id="t3"><span id="sample3">
</span><br></td></td>
</tr>
<tr>
<td>Firstname:</td>
<td colspan="3"><input name="t4" type="text" id="t4"><br></td>
</tr>
<tr>
<td>Lastname:</td>
<td colspan="3"><input name="t5" type="text" id="t5"><br></td>
</tr>
<tr>
<td>Age:</td>
<td colspan="3"><input name="t6" type="text" id="t6"><br></td>
</tr>
<tr>
<td>Email:</td>
<td colspan="3"><input name="t7" type="email" id="t7"><br></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="t8a" id="t8a"><br></td>
<td><input type="text" name="t8b" id="t8b"><br></td>
<td><input type="text" name="t8c" id="t8c"><br></td>
</tr>
<tr>
<td></td>
<td>Lot/Block#</td>
<td>Brgy/Subd</td>
<td>City/Municipality</td>
</tr>
<tr>
<td>Country:</td>
<td colspan="3"><input name="t9" type="text" id="t9"><br></td>
</tr>
<tr>
<td>Contact#:</td>
<td><input name="t10a" type="text" id="t10a"><br></td>
<td><input name="t10b" type="text" id="t10b"><br></td>
</tr>
<tr>
<td></td>
<td>Area Code</td>
<td>Number<td>
</tr>
</table>
<br><br>
<button type="submit" onClick="getText()">Submit</button><br><br>
<p id="log">
</p>
</form>
</center>
</body>