Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Displaying Errors and Passing Data | Working with Forms
Quizzes & Challenges
Quizzes
Challenges
/
PHP Core Concepts

bookDisplaying Errors and Passing Data

When working with forms in PHP, you often need to validate user input and provide meaningful feedback if something is wrong. A robust error handling strategy ensures that users know exactly what needs to be corrected. Typically, you validate input after the form is submitted. If there are errors, you display them on the same page so the user can fix their input. To display errors, you can collect messages in an array and output them above the form fields.

Passing data between pages is another important task. There are several techniques for this in PHP:

  • Using query strings: add data to the URL with ?key=value pairs;
  • Using hidden fields: include extra <input type="hidden"> fields in forms to carry data invisibly;
  • Using sessions: store data on the server, accessible across multiple pages during a user's visit.
index.php

index.php

success.php

success.php

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
<?php session_start(); $errors = []; $name = ''; $email = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Validate name if (empty($_POST['name'])) { $errors[] = "Name is required."; } else { $name = htmlspecialchars($_POST['name']); } // Validate email if (empty($_POST['email'])) { $errors[] = "Email is required."; } elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $errors[] = "Invalid email format."; } else { $email = htmlspecialchars($_POST['email']); } // If no errors, pass data to next page using session if (empty($errors)) { $_SESSION['name'] = $name; $_SESSION['email'] = $email; header('Location: success.php'); exit; } } ?> <!DOCTYPE html> <html> <head> <title>Form Error Handling Example</title> </head> <body> <h2>Registration Form</h2> <?php if (!empty($errors)): ?> <div style="color: red;"> <ul> <?php foreach ($errors as $error): ?> <li><?php echo $error ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <form method="POST" action="index.php"> Name: <input type="text" name="name" value="<?php echo $name ?>"><br><br> Email: <input type="text" name="email" value="<?php echo $email ?>"><br><br> <!-- Hidden field example --> <input type="hidden" name="referrer" value="homepage"> <input type="submit" value="Register"> </form> </body> </html>

Each method has its use cases. Query strings are visible in the browser address bar and are suitable for non-sensitive data. Hidden fields are useful when you want to keep data within forms but do not want users to change it easily. Sessions are ideal for storing sensitive or larger amounts of data that should persist across multiple requests without exposing it in the browser.

question mark

Which of the following is a common way to pass data between PHP pages after form submission?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookDisplaying Errors and Passing Data

Swipe to show menu

When working with forms in PHP, you often need to validate user input and provide meaningful feedback if something is wrong. A robust error handling strategy ensures that users know exactly what needs to be corrected. Typically, you validate input after the form is submitted. If there are errors, you display them on the same page so the user can fix their input. To display errors, you can collect messages in an array and output them above the form fields.

Passing data between pages is another important task. There are several techniques for this in PHP:

  • Using query strings: add data to the URL with ?key=value pairs;
  • Using hidden fields: include extra <input type="hidden"> fields in forms to carry data invisibly;
  • Using sessions: store data on the server, accessible across multiple pages during a user's visit.
index.php

index.php

success.php

success.php

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
<?php session_start(); $errors = []; $name = ''; $email = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Validate name if (empty($_POST['name'])) { $errors[] = "Name is required."; } else { $name = htmlspecialchars($_POST['name']); } // Validate email if (empty($_POST['email'])) { $errors[] = "Email is required."; } elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $errors[] = "Invalid email format."; } else { $email = htmlspecialchars($_POST['email']); } // If no errors, pass data to next page using session if (empty($errors)) { $_SESSION['name'] = $name; $_SESSION['email'] = $email; header('Location: success.php'); exit; } } ?> <!DOCTYPE html> <html> <head> <title>Form Error Handling Example</title> </head> <body> <h2>Registration Form</h2> <?php if (!empty($errors)): ?> <div style="color: red;"> <ul> <?php foreach ($errors as $error): ?> <li><?php echo $error ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <form method="POST" action="index.php"> Name: <input type="text" name="name" value="<?php echo $name ?>"><br><br> Email: <input type="text" name="email" value="<?php echo $email ?>"><br><br> <!-- Hidden field example --> <input type="hidden" name="referrer" value="homepage"> <input type="submit" value="Register"> </form> </body> </html>

Each method has its use cases. Query strings are visible in the browser address bar and are suitable for non-sensitive data. Hidden fields are useful when you want to keep data within forms but do not want users to change it easily. Sessions are ideal for storing sensitive or larger amounts of data that should persist across multiple requests without exposing it in the browser.

question mark

Which of the following is a common way to pass data between PHP pages after form submission?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 6
some-alt