也许你想使用这样的JavaScript函数来检查空字段,而不是匹配的密码(如我把这个从一个小项目我做了相应的请更改变量名):
function signup(){ //Call it on button click
var u = _("username").value;
var e = _("emailAddress").value;
var p1 = _("password").value;
var p2 = _("passwordConfirm").value;
var c = _("country").value;
var g = _("gender").value;
var status = _("status");
if(u == "" || e == "" || p1 == "" || p2 == "" || c == "" || g == ""){
status.innerHTML = "Please, fill in every single field in the form...";
} else if(p1 != p2){
status.innerHTML = "The passwords do not match...";
} else if(_("terms").style.display == "none"){
status.innerHTML = "You must view our Terms & Conditions in order to proceed...";
} else { } //Redirect to a page or use Ajax to do other functions your site may need.
通知var status = _("status");
,这是信息将显示在您的网页上的位置,您可能需要在您的HTML代码中添加<span id="status"></span>
之类的内容。
同样要检查你的数据库上可用的用户名或电子邮件,你可以试试下面的Ajax和JavaScript函数:
<?php
// Ajax calls this NAME CHECK code to execute
if(isset($_POST["usernamecheck"])){
include_once("php_includes/db_con.php"); //database connection file
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']); //checks the texfield doesnt have any funny unusual characters
$sql = "SELECT id FROM users WHERE username='$username' LIMIT 1"; //change table name accordingly to yours
$query = mysqli_query($db_con, $sql);
$uname_check = mysqli_num_rows($query);
//This is just some extra conditions if you wish to add
if (strlen($username) < 3 || strlen($username) > 16) {
echo '<strong style="color:#F00;">3 - 16 characters please</strong>';
exit();
}
if (is_numeric($username[0])) {
echo '<strong style="color:#F00;">Usernames must begin with a letter</strong>';
exit();
}
//This if statement will check if the username is ok to use or is taken.
if ($uname_check < 1) {
echo '<strong style="color:#009900;">' . $username . ' is OK</strong>';
exit();
} else {
echo '<strong style="color:#F00;">' . $username . ' is taken</strong>';
exit();
}
}
?>
//////////// JavaScript函数,这些可以在同一个php文件中。
function checkusername(){
var u = _("username").value;
if(u != ""){
_("usernamesats").innerHTML = 'Checking Availability...';
var ajax = ajaxObj("POST", "signup.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
_("usernamesats").innerHTML = ajax.responseText;
}
}
ajax.send("usernamecheck="+u);
}
}
注意,给你看的警告,您的用户名文本字段必须是这样的:<label>Username:</label> <input id="username" type="Text" onBlur="checkusername()" maxlength="16">
这onBlur="checkusername()"
将调用JS功能。
并且还在texfield后面添加这个<span id="usernamesats"></span>
来显示警告。这应该都是诀窍。哦,你可能想要添加Ajax文件:
function ajaxObj(meth, url) {
var x = new XMLHttpRequest();
x.open(meth, url, true);
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
return true;
}
}
某处在您的js文件夹中(如果有的话)。
对不起,如果这可能会有点长和困惑,但它为我做了工作,我相信还有其他方法可以做到这一点,只是认为这些对我来说似乎更容易理解。
查看本网站http://www.developphp.com/list_php_video.php欲了解更多信息,有一些伟大的教程,让你开始使用PHP和MySQL,大部分代码完成后,教程:) :)祝你好运!
我可以使用goto,但我知道这是不好的做法? –
|不是有效的操作符,请使用||或“或”。 –
使用http://stackoverflow.com/questions/6748499/required-field-display-error-message-on-form作为起点。 – DCoder