Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2036
  • Last Modified:

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

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
justaj
Asked:
justaj
1 Solution
 
justajAuthor Commented:
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
 
AielloJCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now