php variables inside html error


#1

Hi,

I am trying to create a variable in PHP that will represent an error message and in the case of an error the message will appear inside the HTML.

If you examine my code you will notice that on line 69 I have the following code:

<span style="color: red;">
  <?= $Error_message; ?>
</span>

However this is not working correctly and I received the following error:

Undefined variable: Error_messag in C:\xampp\htdocs\eli_task\Connect.php on line 69.

For your convenience we are attaching our code to this form.

<?php

session_start();

if( isset($_SESSION[‘uid’]) || isset($_COOKIE[‘rememberme_id’])){

header(‘location: members.php’);

}

$user = [

‘id’ => 56,

‘name’ => ‘shimi’,

‘email’ => ‘shim@gmail.com’,

‘password’ => ‘123456’,

];

$error = ‘’;

$emailRegex = “/^[\w-]+(.[\w-]+) @[a-z0-9-]+(.[a-z0-9-]+) (.[a-z]{2,3})$/i”;

if(isset($_POST[‘sUbmit’])){

if(empty($_POST[‘eMail’]) || !preg_match ($emailRegex , $_POST[‘eMail’] ))

{

$Error_message = “Your email pattern is problematic or empty”;

}

elseif (empty($_POST[‘pOsword’]) || empty($_POST[‘fIrstname’])){

$Error_message = “You did not enter a password or a Name”;

}

elseif($user[‘name’]<>$_POST[‘fIrstname’] ){

$Error_message =“Your name does not equal the name on the system”;

}

elseif($user[‘email’]<>$_POST[‘eMail’] || $user[‘password’] <>$_POST[‘pOsword’]) {

$Error_message =“You have an issue with your password or email”;

}

}

?>

<!DOCTYPE html>

<html lang=“en“>

  <head>

    <meta charset=“UTF-8“>

      <meta name=“viewport“ content=“width=device-width, initial-scale=1.0“>

        <meta http-equiv=“X-UA-Compatible“ content=“ie=edge“>

          <title>Welcome to my page</title>

        </head>

  <body>

    <div id=“signin-form-wrapper“>

      <form method=“post“ action=“Connect.php“ novalidate=“novalidate“>

        What is your name?:<br>

          <input type=“text“ name=“fIrstname“>
            <br>

              What is your email:<br>

                <input type=“email“ name=“eMail“ required=““ placeholder=“Enter a valid email address“>
                  <br>

                    What is your Pasword:<br>

                      <input type=“password“ name=“pOsword“>
                        <br>

                          <br>

                            <input type=“submit“ name=“sUbmit“ value=“Submit“>

                              <span style=“color: red;“>
                                <?= $Error_message; ?>
                              </span>

                            </form>

    </div>

  </body>

</html>

Looking forward for your help.


#2

error_message is not defined. You can check it first with isset() or suppress that notification with @. HOWEVER, since you are also coloring the text, isset is what you would want to do…

<?php if(isset($error_message)): ?>
<span style="color: red;">
  <?= $Error_message; ?>
</span>
<?php endif; ?>

#3

@astonecipher, I am going to have to say no on the whole reply except the first sentence. I am really surprised you are suggesting error suppression. Also, isset is not the answer either. OP already has an empty string for $error so it would already be isset (Should actually be $error_message or better yet, change $error_message to $error).

OP, There are many issues with this code.

  1. You need to kill the script after header redirects or the script will continue to run.

  2. Stop with mixing your case all over the place. Just name all your variables and field names all lowercase and separate words with_an_underscore.

  3. Depending on the name of a button to be submitted in order for your script to work will fail in certain cases. You need to check the REQUEST METHOD.

  4. Your errors should be an array and shown all at once. To display errors you simply check if the errors array is empty or not. If its not empty, foreach over the errors for display.

  5. Your form doesn’t even do anything if there are no errors. I am assuming you are just trying to learn how to do Php forms.

  6. Remove the action completely. Page will submit to itself.


#4

That is because of the usage.

<?=$error?>

will throw an error when it isn’t set, but

<?=@$error?>

is cleaner in this case, for that purpose ONLY; not to suppress other types of issues.


#5

Well, first we need recognize the OP’s bad coding. He set $error to an empty string which would have been OK if he had named the errors the same, but he didn’t, he named them $Error_message which is why he has the problem in the first place. If they were named the same, there would be no error.

As far as the OP’s approach to the error handling, the error messages will overwrite each other and only give the last error message in the case that there are two or more errors at the same time. Obviously that is yet another problem.

As I posted and I am sure you have seen or perhaps done yourself is that the $error variable should be an empty array, not an empty string and definitely not suppressed. Then each error should be an array. Now no errors are overwritten and they can all be displayed at once by foreach’ing over the error array.

As you are aware, the @ is an error suppressor which means, if you need it there is a bad coding problem that you are covering up with it, which I have explained what that coding problem is. Hiding bad code is not clean. The clean option is for it to be written correctly.


#6

seeing you can really have multiple error messages I’d change it to something like this

$errors = [];

if(!$condition) {
  $errors[] = 'Error message for condition 1';
}


if(!$condition2) {
  $errors[] = 'Error message for condition 2';
}
//...
?>
<input type=“submit“ name=“sUbmit“ value=“Submit“>
<?php foreach ($errors as $error): ?>
  <p class="error"><?= $error ?></p>
<?php endforeach; ?>
</form>

It solves the problem of not having the variable properly set, it allows you to display all the errors at once instead of the user having to fix each one with a form submit/page refresh between each

edit: haha @benanamen, didn’t see that you already sketched this up. Well uh yeah, so what this guy said ^


#7

Thank you for your help this helps