0

I would like to apply a css class (.error) to every echoed instance of $error. It seems like an easy thing to do, I'm just not sure about the syntax.

<?php // rclogin.php
    include_once 'rcheader.php';
    echo "<h3>Member Log in</h3>";
    $error = $user = $pass = "";

    if (isset($_POST['user']))
    {
        $user = sanitizeString($_POST['user']);
        $pass = sanitizeString($_POST['pass']);

        if ($user == "" || $pass == "")
        {
            $error = "Not all fields were entered<br />";
        }
        else
        {
            $query = "SELECT user,pass FROM rcmembers
                      WHERE user='$user' AND pass='$pass'";

            if (mysql_num_rows(queryMysql($query)) == 0)
            {
                $error = "Username/Password invalid<br />";
            }
1
  • Your code does not contain the error class anywhere. Into which concrete problem did you run? Commented Jan 30, 2012 at 13:32

3 Answers 3

3

First of all - do not put into $error a whole HTML. It's a bad habbit in my opinion. Place there just an error info:

$error = 'Not all fields were entered';

And then in HTML during printing error info:

if ( $error ) {
    echo '<span class="error">' . $error . '</span><br/>';
}

As you can see - if you pass HTML in variables, you have less opportunities to do everything you want.

Sign up to request clarification or add additional context in comments.

Comments

2

To suggest something slightly different, I like to implement my errors as a class.

Now as it's a class, I can use the __toString() magic method.

class Error
{
    ...

    public function __toString()
    {
        ...
        $output = '<span class="error">' . $output . '</span>';
        return $output;
    }
}

$error = new Error;

Now when you echo $error, you can specify some HTML to be applied around the message, and as its a class, you can contain all of your other related error functions together in a nice object.

Comments

0

Of course it is possible depending on how you want to do it.

You could have something like this $error = "<span class='className'>Username/Password invalid</span>";

or you could first create the error variable with $error = "<span class='className'>" and end it with $error .= "</span>"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.