0

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 some errors occur, I want to display them using PHP on the page:

<h2>Contact</h2>
<?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. So basically, if I remove the header() function, the page goes back to the top, and the errors show; if I keep the header() function, the page correctly stays where it is, but no errors show.

Alternatively, this question also can be asked in the case of, what happens when 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!

1
  • 1
    The best way to do this would be via ajax. Commented Dec 7, 2012 at 22:41

2 Answers 2

2

Specifying a location in the header will cause the browser to redirect to that URI. This is being called before any output is sent to the browser.

When the page is reset, $errors == false, so the paragraph isn't displayed.

It sounds like you need to integrate AJAX, or a mixture of server- and client-side code here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. I have gone ahead and used JavaScript for client-side validation, as it preserves the page location when you prevent the default behavior of the form when errors are found.
1

A very simple and efficient way to validation of a form is using jquery, A perfect example of this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
 <style type="text/css">
* { font-family: Verdana; font-size: 96%; }
label { width: 10em; float: left; }
label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
p { clear: both; }
.submit { margin-left: 12em; }
 em { font-weight: bold; padding-right: 1em; vertical-align: top; }
 </style>
 <script>
  $(document).ready(function(){
    $("#commentForm").validate();
   });
   </script>

 </head>
 <body>


 <form class="cmxform" id="commentForm" method="get" action="">
 <fieldset>
  <legend>A simple comment form with submit validation and default messages</legend>
  <p>
 <label for="cname">Name</label>
 <em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />
  </p>
  <p>
 <label for="cemail">E-Mail</label>
 <em>*</em><input id="cemail" name="email" size="25"  class="required email" />
  </p>
  <p>
 <label for="curl">URL</label>
 <em>  </em><input id="curl" name="url" size="25"  class="url" value="" />
     </p>
   <p>
 <label for="ccomment">Your comment</label>
 <em>*</em><textarea id="ccomment" name="comment" cols="22"  class="required"></textarea>
  </p>
   <p>
 <input class="submit" type="submit" value="Submit"/>
  </p>
  </fieldset>
  </form>
 </body>
 </html>

Happy Coding.!!!

1 Comment

Thanks for this. I have gone ahead and written a few small JS programs to validate my simple form. It works well because preventing the default behavior of a form until no errors are found maintains the page location, which is what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.