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,017 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
2 Comments
 

Author Comment

by:justaj
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 …
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now