[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


PHP form - validate, show errors on the same page and if all ok, show results on another page - beginner needs lots of help

Posted on 2008-09-29
Medium Priority
Last Modified: 2013-12-12
Hi, I've created a simple form and all I want is:
- If the request isn't a form submission, show the form.
- If the request is a form submission, check for required fields and validate the submitted parameters.
- If the submitted parameters are valid, process the form - got to a separate page and display results there.
- If the submitted parameters are invalid, show the form on the same page with the missing fields' tags in red.

I am beginner in php, tried lots of options I found on the web or in the books, but I can't move on.
Please help.
My form.php and thankyou.php pages are below (there is lots of, I think, unnecessary code there, but I was trying everything).
# form.php file #
// when going first time to the form display empty fields
	$name = "";
	$town = "";
	$email = "";
	$comments = "";
function check_input($data, $problem='')
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    return $data;
// if send is check if the required fields are filled - name and email
if (array_key_exists('send',$_POST)) {
	if (!(isset($_POST['name']) && strlen($_POST['name'])))
		$errors[ ] = 'Please enter your name';
        $pattern = '/.*@.*\..*/';
        $email   = $_POST['email'];
	if (!(isset($_POST['email']) && strlen($_POST['email'])) && (preg_match($pattern, $_POST['email']) > 0))
		$errors[ ] = 'Please enter your valid email';
	// if fields are empty a message appears on the page and check the input of all fields
	$message = "Please enter the missing information";
#	if - I need an if statement here to check the input if user filled a field or I probably could combine it with the checking for required fields
#	{
#		$firstname = check_input($_POST['name'],"Enter your first name");
#		$town = check_input($_POST['town']);
#		$email = check_input($_POST['email'],"Enter your email");
#		$comments = check_input($_POST['comments']);
#	}
	// display the information that the user filled already in and highlight the missing information in different font, red for example
        $name = $_POST['name'];
        $town = $_POST['town'];
        $email = $_POST['email'];
        $comments = $_POST['comments'];
#	if all ok - go to the thank you page and display the results there 
#	{
#		header('Location: http://localhost/.../thankyou.php');
#	exit;
#	}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Form test</title><style>
    .errortext {
        font-family: Arial, Helvetica, sans-serif; 
        font-size: 14px; color:#C00000; font-weight: bold;
	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
		<p><strong>Please fill in the form</strong> <span class="small">(fields marked * are mandatory)</span>:</p>
		<?php if ($message != "") {
		       	print "<span class='errortext'>" . $message . "</span>";
		<p><label>Name*:</label> <input type="text" size="65" name="name" value="<?php print $name; ?>" /></p>
		<p><label>Town:</label> <input type="text" size="65" name="town" value="<?php print $town; ?>" /></p>
		<p><label>Email*:</label> <input type="text" size="65" name="email" value="<?php print $email; ?>" /></p>
		<p>Additional comments:<br />
		<textarea rows="3" cols="50" name="comments" value="<?php print $comments; ?>">&nbsp;</textarea></p>
		<p><input class="button" type="submit" name="send" id="send" value="Submit" /></p>
# thankyou.php file #
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Thank you page</title></head>
	<p>Thank you for your comments</p>
	<p>Your details:</p>
		print $name . "<br />";
		print $town . "<br />";
		print $email . "<br />";
		print "<br />Additional comments:<br />" . $comments; 

Open in new window

Question by:justaj

Author Comment

ID: 22603691
Maybe we can strat with how to combine the checking for empty fields and malicious entries and getting the errors on the page (for example by changing 'name' to red font.

LVL 13

Accepted Solution

AielloJ earned 2000 total points
ID: 22604653
Most of your code appears to be commented out.  I don't have time to load up what you have but willing to help.  What is happening when you run your page currently?  At first glance, it seems like all your page variables are being set to blanks every time you goto the page according to the comment at the top that reads: " // when going first time to the form display empty fields"

A couple of suggestions:

1) Put all your php code into an include file.  It's a little confusing to see php mixed with HTML.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question