如何以注册形式显示错误消息?

时间:2013-08-02 11:13:03

标签: php mysql

如何显示错误消息(您没有完成所有必填字段并且已经使用了此用户名)而没有进入新页面来显示它们(比如使用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. ');
    }


} ?>

2 个答案:

答案 0 :(得分: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");,此消息将显示在您的网页上,您可能需要在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 this <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,大部分代码都是按照那里的教程完成的:)祝你好运!

答案 1 :(得分: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"
}

} ?>