42: How to Display Error Messages Using PHP | PHP Tutorial | Learn PHP Programming



How to Display Error Messages Using PHP | PHP Tutorial | Learn PHP Programming. In this PHP tutorial, you will learn how to show the user an error message using PHP, after they submit a form on your website. This is done by sending data back to the form using PHP validation.

We will also cover how to keep the data in the form inputs, if the user is sent back with an error, so they don’t need to re-type everything.

➤ GET ACCESS TO MY LESSON MATERIAL HERE!

First of all, thank you for all the support you have given me!

I am really glad to have such an awesome community on my channel. It motivates me to continue creating and uploading content! So thank you!

I am now using Patreon to share improved and updated lesson material, and for a small fee you can access all the material. I have worked hard, and done my best to help you understand what I teach.

I hope you will find it helpful 🙂

Material for this lesson:

Nguồn: https://svdpch.org/

Xem thêm bài viết khác: https://svdpch.org/cong-nghe/

23 thoughts on “42: How to Display Error Messages Using PHP | PHP Tutorial | Learn PHP Programming”

  1. Hi, Daniel! Thank you for the lession! There is a nuance though. If you leave any of the fields empty (say, password) and click submit, it still erases all fields. Not the best UX. Could you kindly advise? Thanks!

    Reply
  2. thank you for doing this series again it's not that you did it terribly here its just you explained the html/css series way better

    Reply
  3. Thanks Daniel for the video, one question though: if the user entered invalid email and a username already in use. The signup form will throw back the error as invalid email only as it will exit with the email error in the URL before running the username handler on the signup.inc.php and it will return the username into the signup form to the user. How can we let the signup.inc.php file check the user first and last name, email and username before returning the error and how to have a combination of errors before directing the user to the signup.php?

    Reply
  4. If anyone's including the 'preg_match' bit in their code but doesn't understand what all those weird symbols are for, like me, check this link out: https://www.jotform.com/blog/php-regular-expressions/

    Reply
  5. Does the data still save into the database even though the data that the user was type is invalid? Coz i noticed in my current project that incorrect data still get save in my database? How can i avoid it?

    Reply
  6. Why don't you use a switch?

    if(isset($_GET['signup']))

    {

    if($_GET['signup'] == 'success') {

    echo "<p class='success'>Signup successful</p>";

    } else {

    echo "<p class='error'>";

    switch($_GET['signup'])

    {

    case 'check':

    echo "You came into this page from an invalid link";

    break;

    case 'empty':

    echo "Fill all required fields";

    break;

    case 'email':

    echo "Invalid email";

    break;

    case 'username':

    echo "This username already exists";

    break;

    case 'connection':

    echo "Impossible to connect with the server";

    break;

    default:

    echo "Unknown error";

    break;

    }

    echo "</p>";

    exit();

    }

    }

    Reply
  7. One problem is that the value doesnt save if using a textarea tag, maybe this is because value attribute works different with this tag? Anyone know how to fix…

    Reply
  8. When the page is loaded you can actually get the URL parameters by simply using $_GET[param]. Then you can just do it like:

    if ($_GET['signup'] === 'empty') {
    echo '<p class="error">You did not fill in all fields</p>';
    }

    or do it like;

    $message = $_GET['signup'];

    if ($message === 'empty') {
    echo '<p class="error">You did not fill in all fields</p>';
    }
    …and so on…

    Reply
  9. the problem is today google wont let me click the submit button uless the email field has a @ and a ".whatever" and when i make up an email it says its a valid email i have to change the input type to a text type which solves the problem. but i dont like it shrug

    Reply
  10. Hi,
    I am not able to get back the input data from url if I use Textbox as input type instead of input.
    Below give code is giving back data enter from the url
    if(isset($_GET['siname'])) {

    $siname = $_GET['siname'];

    echo '<input type="text" id="SI" name="SIName" placeholder="System Integrator:" value="'.$siname.'">';

    }

    But Below is not giving
    if(isset($_GET['solution'])) {

    $solution = $_GET['solution'];

    echo '<textarea id="solution" name="solution" rows="10" cols="30" placeholder="Solution Provided…."value="'.$solution.'"></textarea>';

    }

    Reply
  11. I've copied your code exactly for the signup.inc.php page, but when I press on the Signup button, I get the following error message: Parse error: syntax error, unexpected '^' in… . It won't accept the carat "^". in the second if statement. What am I doing wrong? How to correct it? My code: if (!preg_match(¨/^[a-zA-Z]*$/", $first) || !preg_match(¨/^[a-zA-Z]*$/", $last)) {
    header("Location: ../index.php?signup=char"); exit();

    Reply
  12. did not work for me, I'm getting an redirect loop, cause of the header location redirect to the same page, idk how to fix that

    Reply

Leave a Comment