James G.
James G.

Reputation: 2904

How do I display the error messages in signup form?

How do I display the error messages (you did not complete all of the required fields and this username is already taken) without going to a new page to display them (like using die instead of echo), while still not continuing the process? In other words, I don't want the user to be sent to a new page to see "you did not...," I want the error to show either below or above the on this page, but I want the error message to disallow the data from being added to the database(the next command, or a couple commands down).

//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. ');
    }


} ?>

Upvotes: 1

Views: 670

Answers (2)

Balram Singh
Balram Singh

Reputation: 1744

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

} ?>

Upvotes: 1

Scur4
Scur4

Reputation: 154

Maybe you wanna use something like this javascript function to check for empty fields and not matching passwords (change variable names accordingly please as I took this from a little project I made):

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.

Notice var status = _("status");, this is where the messages will be shown on your page, you may want to add something like <span id="status"></span> to your HTML code.

Similarly to check for an available username or email on your database, you can try the following Ajax and Javascript function:

<?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 function, these can be on the same php file.

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);
  }
}

Notice that for you to see the warnings your username textfield must look like this: <label>Username:</label> <input id="username" type="Text" onBlur="checkusername()" maxlength="16">

This onBlur="checkusername()" will call the JS function.

and also add somewhere after the texfield this <span id="usernamesats"></span> to display the warnings. This should all do the trick. Oh and you may want to add the Ajax file:

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

somewhere in your js folder (if you have one).

Sorry if this may be a bit long and confusing but it did the work for me, I'm sure there are other ways to do it, just thought these seemed easier to understand for me.

Check this website http://www.developphp.com/list_php_video.php for more info, there are some great tutorials there to get you started with PHP and MySQL, most of this code was done following the tutorials there :) Good Luck!

Upvotes: 1

Related Questions