Home > other >  How to return status message to form [PHP]
How to return status message to form [PHP]

Time:02-17

So, in my corePHP Project I have sign-up.php page with form:

<form method="POST" action="./Controllers/RegistrationController.php">

Now, in this RegistrationController.php I have

$errors=Validator::validateUser($data);
if(count($errors)==0)
{
    //here I will add code to insert user to database later
    
}
else{
    // What code to write here?
}

Basically, if array of $errors is empty, data is valid, and user will be inserted into db, otherwise errors will be displayed on sign-up.php page. My question is how to return these $errors to sign-up.php and show them in my starting form?

CodePudding user response:

You can redirect user to your sign-up.php with sending HTTP-header. Just use header() for this.

You can set your errors as query params, like http://example.com/sign-up.php?error[field]=name&error[message]=empty!

And for generate beautiful params, try to use http_build_query()

In your case:

$errors=Validator::validateUser($data);
if(count($errors)==0)
{
    // some success things
}
else{
    // I just put all array as params for example
    header('Location: http://www.example.com/?' . http_build_query($errors));
    exit; 
}
  •  Tags:  
  • php
  • Related