如何在注册表单中显示错误消息


How do I display the error messages in signup form?

如何在不转到新页面显示错误消息(例如使用die而不是echo)的情况下显示错误消息?换言之,我不希望用户被发送到一个新页面,看到"你没有…",我希望错误显示在该页面的下方或上方,但我希望错误消息不允许将数据添加到数据库中(下一个命令或几个命令)。

//if submit is clicked
if (isset($_POST['submit'])) {
    //then check if all fields are filled
    if (empty($_POST['username']) | empty($_POST['password']) | empty($_POST['firstname']) | empty($_POST['MI']) | empty($_POST['lastname']) | empty($_POST['email']) | empty($_POST['phonenumber']) | empty($_POST['country']) ) {
        echo('You did not complete all of the required fields. '); }
    $username = $_POST['username'];
    $password = $_POST['password'];
    $usernamesquery = mysqli_query($connection, "SELECT * FROM users WHERE username='$username'");
    if(mysqli_num_rows($usernamesquery) > 0) {
        echo('This username is already taken. ');
    }

} ?>
//if submit is clicked
if (isset($_POST['submit'])) { 
$username = $_POST['username'];
$password = $_POST['password'];
$usernamesquery = mysqli_query($connection, "SELECT * FROM users WHERE username='$username'");
//then check if all fields are filled
if (empty($_POST['username']) | empty($_POST['password']) | empty($_POST['firstname']) | empty($_POST['MI']) | empty($_POST['lastname']) | empty($_POST['email']) | empty($_POST['phonenumber']) | empty($_POST['country']) ) {
    echo('You did not complete all of the required fields. '); }
elseif(mysqli_num_rows($usernamesquery) > 0) {
    echo('This username is already taken. ');
}
else{
  echo "code to submit values to database"
}
} ?>

也许你想使用类似于这个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之后的某个地方添加该CCD_ 5以显示警告。这一切都应该奏效。哦,你可能想添加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,这些代码大部分都是按照教程完成的:)祝你好运!