Jump to content
Larry Ullman's Book Forums
Sign in to follow this  

Simple Question On Php Form Validation...

Recommended Posts

Hi there,


I have a relatively simple sounding question that hopefully entails a simple answer. I am currently developing a website that is a scroll-down type; everything is on one page, you know the one. My 'contact' section is at the very bottom of the page, and I am of course doing form validation with PHP. The validation part works, I have no trouble with that. However, if validation fails, the browser takes me back all the way back to the top of the page; this is inconvenient for obvious reasons.


I am aware of the 'header()' function, which I can use to keep me at the bottom of the page if an error occurs. As an example, at the top of my HTML page before the DOCTYPE, I can write:


if ($errors) {

header(Location: 'somelocation.php#contact');



This works, but for some reason it prevents my PHP embedded in my html to work. If I have:



<?php if($errors) { ?>

<p class="warning">Please fix the errors</p>

<?php } ?>


'Please fix the errors' does not appear. It does appear however, if I remove the header function from the page, so I know the problem is related.


Alternatively, this question also can be asked in the case of, what happens if the form is validated correctly, the 'header' function is called, and I want to stay at the bottom of the page and display some HTML saying 'Thanks, your form has been submitted'? (I don't want to go to a 'thank-you' page or anything, just stay in the same spot and give a 'thanks' on the page) I assume I'd run into the same problem. Is there a solution to this?


Thanks in advance!

Share this post

Link to post
Share on other sites

I have a script that processes the mail, in an external file. However, I use a self-processing form as well (instead of sending the data to a separate file, there is a PHP conditional statement above the DOCTYPE declaration that checks if the form has been submitted). I'm doing this because I want to preserve user input on the page if there are errors.


The parts of the script that are specific to the form are embedded in the PHP code block on the .php page itself. The reusable parts are in a separate file.

Share this post

Link to post
Share on other sites

When you execute the header(Location: 'somelocation.php#contact'); line, you're reloading the page, which causes the $_POST array, $errors variable, etc. to no longer exist. This essentially amounts to the same thing as having never performed any validation at all (and thus the $errors variable not existing, which is why your "Please fix the errors" line is never printed out).


A better solution would be to put the anchor jump (i.e., the fragment part in the URL after the number sign (#)) in the URL specified for the action attribute of the contact form. For example, if the following is the opening tag for your contact form:


<form action="somelocation.php" method="post">


Then change it to the following:


<form action="somelocation.php#contact" method="post">


Of course, this assumes that somelocation.php refers to the original page the form is on (which seems to be the case according to what you said).

By doing the above, you can very easily get the jump you want and still properly validate everything.


In terms of outputting a thank you message upon successful validation, I'd do something like the following:


if (!$errors) {

 // Validation was successful. Output a thank you message.

} else {

 // Either there was an error or this is the first time the page was loaded.
 // Load the page with the form and any necessary sticky values and error messages.



Hope that helps.

  • Upvote 3

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...