我创建了一个基本的注册表单,可以使用全部功能。唯一的问题是出现的错误在屏幕的左上角很远,看起来很尴尬。与它一起看起来它与显示错误的文本框无关。我希望错误消息以特定文本框中特定消息的方式显示在工具提示中。
如果用户成功登录,我希望他将其重定向到登录页面,但在我的网页中它因此声明而死亡。
die ("<h2> Welcome</h2>Login to get started...");
请为此提供替代方案。任何帮助肯定会受到赞赏。
if ($reg) {
if ($em==$em2) {
$e_check = mysql_query("SELECT email FROM users WHERE email='$em'");
$checke = mysql_num_rows($e_check);
$u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
$check = mysql_num_rows($u_check);
if ($check == 0 && $checke == 0) {
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2) {
if ($pswd==$pswd2) {
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "Username/ First Name/ Last Name cannot exceeds 25 characters!";
}
if (strpos($un, " ") !== false){
echo "Your username cannot have a space character.";
}
if (strpos($em, "@") == false){
echo "Invalid Email";
}
else {
if(strlen($pswd)>30||strlen($pswd)<5) {
echo "Your password must be between 5 and 30 characters long!";
}
else{
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO users VALUES ('', '$un','$fn','$ln','$em','$pswd','$d','0','Write something about yourself.','','')");
die ("<h2> Welcome</h2>Login to get started...");
}
}
}
else{
echo "Your passwords doesn't match!";
}
}
else{
echo "Please fill in all the fields";
}
}
else{
echo Username already taken or Email already registered
}
}
else{
echo "Your E-mails doesn't match";
}
}
HTML:
<form action="#" method="POST">
<input type="text" name="fname" size="25" placeholder="First Name" />
<input type="text" name="lname" size="25" placeholder="Last Name" /><br><br>
<div data-tip="Make Sure you remember your username. You'll need it at the time of login. Your username should not have space in between.">
<input type="text" name="username" size="56" placeholder="User Name" /></div><br><br>
<input type="text" name="email" size="56" placeholder="Email Address" />
<input type="text" name="email2" size="56" placeholder="Please re-enter your Email Address" /><br><br>
<input type="password" name="password" size="47" height="30" padding="30" placeholder="Password" /><br><br>
<input type="password" name="password2" size="47" height="30" placeholder="Please re-enter your Password" /><br><br>
<input type="submit" name="reg" value="Sign Up and learn!" />
</form>
答案 0 :(得分:0)
如果要自定义错误消息,请说明为红色 什么的您可以为所有span块添加一个类。你可以 指定该类的属性。如下所示
<style>
.help-block
{
color: red;
}
</style>
您可以在每个输入下面添加一些span块。每个输入都有一个唯一的ID,并将其隐藏起来 默认。喜欢下面
<input type="text" name="fname" size="25" placeholder="First Name" />
<span class="help-block" id="fname_block" style="display:none;"></span><br>
<input type="text" name="lname" size="25" placeholder="Last Name" />
<span class="help-block" id="lname_block" style="display:none;"></span><br>
在PHP中:
如果第一个输入中有错误。即,fname
echo"<script>document.getElementById('fname_block').innerHTML = 'error in first name';
document.getElementById('fname_block').style.display = 'inline'; </script>";
或者如果错误是第二个输入(即lname)。然后在代码的getelementById
部分中指定该输入的span块的id
如果没有错误,请不要忘记隐藏span块。
document.getElementById('fname_block').style.display = 'none';
如果你使用jquery(你只需要添加jquery库),编码 会很简单。
上面相同js代码的jquery代码
$('#fname_block').html('error in first name').show();
你可以在jquery中隐藏一个元素,如下所示。
$('#fname_block').hide();