1

如何在不转到新页面显示错误消息(例如使用 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. ');
    }


} ?>
4

2 回答 2

1

也许你想使用类似这个 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");,这是消息将显示在您的页面上的位置,您可能希望在<span id="status"></span>HTML 代码中添加类似的内容。

与检查数据库中可用的用户名或电子邮件类似,您可以尝试以下 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,大部分代码都是按照那里的教程完成的 :) 祝你好运!

于 2013-08-02T12:18:55.780 回答
1
//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"
}

} ?>
于 2013-08-02T12:24:42.793 回答