0

** $errors array is the erros that will give when the user enters invalid form data, so i wish to style the $errors array below? how can i do that ? like i want to colour it and do some styling **

if($_SERVER['REQUEST_METHOD'] == "POST"){
    $errors = array(); // create an errors array to record errors if any.
        // check if the name is provided and is valid
        if(empty($_POST['Make1'])) {
        $errors[] = 'Make 1 is required.'; // if name is required
        } else {
        $Make1 = trim($_POST['Make1']);
        if (!preg_match("/^[a-zA-Z ]*$/",$Make1)) {
        $errors[] = "Invalid Make1! use only letters and white space.";
        }}
        if (empty($_POST['Make2'])) {
            $errors[] = 'Make 2 is required.';
            } else {
            $Make2 = trim($_POST['Make2']);
              if (!preg_match("/^[a-zA-Z]*$/",$Make2)) {
            $errors[] = "Invalid Make 2! use only letters and white space.";
    }}
13
  • Wrap each error into a div.error echo implode(PHP_EOL, array_map(fn($e) => "<div class=error>{$e}</div>", $errors)); Commented Mar 27, 2021 at 22:48
  • is there any easier way? Commented Mar 27, 2021 at 22:58
  • What is complicated on that? You could write it as an foreach loop which does the same. Commented Mar 27, 2021 at 22:59
  • i didnt uunderstand this bit "echo implode(PHP_EOL, array_map(fn($e) =>" Commented Mar 27, 2021 at 23:04
  • $error is an array. Array_map iterates over it and returns for each error a div. Implode joins that returned new array with a PHP_EOL for each entry. Why don't you test and debug that? Commented Mar 27, 2021 at 23:08

1 Answer 1

1

You can iterate over your $errors array and wrap each message in a <div> that you can style with CSS. (Code thanks to Markus Zeller in the comments)

echo implode(PHP_EOL, 
         array_map(
           function($e) { return "<div class=error>{$e}</div>"; }, 
           $errors
         )
     );

In this snippet array_map() iterates over the $errors array and returns an array with each element updated by the callback function. The callback function wraps each message from the array in a <div>, including a CSS class for styling. Finally, the newly created array is imploded to give a single string to echo.

Input:

$errors = ["Error message 1", "Error Message 2"];

Output:

<div class=error>Error message 1</div>
<div class=error>Error message 2</div>

Now style it by adding some CSS:

.error {
    color:red;
    border:1px solid red;
    background-color:pink;
}
Sign up to request clarification or add additional context in comments.

2 Comments

ok so its working now but i have like so many errors to display so do i have to write this code so many times or is there an easier way that i can call the function or something?
This is a one-time piece of code that you run after you've found all your errors. There are other ways you might do this, including using HTML attributes on your <input> elements and the browser handles it all. You clearly don't understand what you're doing, and Stack Overflow is not the place for tutorials. Work through the one line of code we've provided and understand it. Then ask questions.

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.