2013-08-02 86 views
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. '); 
    } 


} ?> 
+0

我可以使用goto,但我知道这是不好的做法? –

+0

|不是有效的操作符,请使用||或“或”。 –

+0

使用http://stackoverflow.com/questions/6748499/required-field-display-error-message-on-form作为起点。 – DCoder

回答

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" 
} 

} ?> 
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后面添加这个<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,大部分代码完成后,教程:) :)祝你好运!

+0

为了增加客户端检查,我可能会在未来添加JS,但现在,我对php很满意。虽然谢谢! –

+0

没问题,祝你好运:) – Scur4