Solved

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
2
2,027 Views
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 #
#################
 
<?php
session_start();
 
// 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)
    {
        die($problem);
    }
    return $data;
}
 
// if send is check if the required fields are filled - name and email
if (array_key_exists('send',$_POST)) {
 
	//name
	if (!(isset($_POST['name']) && strlen($_POST['name'])))
	{
		$errors[ ] = 'Please enter your name';
	}
 
	//email
        $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;
    }
</style></head><body><div>
	<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>
	</form>	
</div></body></html>
 
#####################
# thankyou.php file #
#####################
<?php
session_start();
?>
 
<!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>
<body><div>
	<p>Thank you for your comments</p>
	<p>Your details:</p>
	
	<?php
		print $name . "<br />";
		print $town . "<br />";
		print $email . "<br />";
		print "<br />Additional comments:<br />" . $comments; 
	?>
</div></body></html>

Open in new window

0
Comment
Question by:justaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:justaj
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.

Thanks
0
 
LVL 13

Accepted Solution

by:
AielloJ earned 500 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.
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 dynamically set the form action using jQuery.

696 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