Skip to main content
June 24, 2011
Question

Only display a DIV what visited with a?redo=true

  • June 24, 2011
  • 1 reply
  • 384 views

Hey forum

I have a contact form that uses PHP. If any of the fields are left blank, it will reload the webpage, with some form of tag on the end (eg contact.php#redo or contact.php?name=valid&email=false&comment=valid) in order to display a message corresponding to the form field that was left blank. I was wondering how I would pull this off?

As I say above there would be three tags as there are three fields- ?name= ?email= and ?comment=

And then the php would have to get[name], get[email] and get

This topic has been closed for replies.

1 reply

June 24, 2011

You can validate form input 2 ways, either on the client side or the server side.

Client side I would suggest just looking at the Spry Validation, the box will glow an evil red if left blank  and the person can correct the error without the form being submitted.

There is an advantage to this in that you don't need to make the form "sticky" ( when the original data is inserted into the input box after a submisson so the submitter does not have to type it all over again) plus the error is immediate and they dont have to wait for server response.

You can also server side scripting where they would hit the submit button and then they would recieve an error message from the server, you can use some of the following

If(isset($_POST['name') && isset($_POST['email'])) {

...}

Which basically says "if the post name and email are set" then continue.

More reading

http://php.net/manual/en/function.isset.php

You can also use

if(!empty($_POST['name']))

Which basically says , if the post name is not empty.

More reading

http://php.net/manual/en/function.empty.php

I would suggest the Spry approach.

Gary

David_Powers
Inspiring
June 26, 2011

garywpaul wrote:

I would suggest the Spry approach.

I wouldn't, at least not on its own. Client-side validation is simply a courtesy to the user, preventing a form from being accidentally submitted with incomplete information. However, it is easily avoided by malicious users, who can either turn off JavaScript in the browser, or create their own form to submit data.

Server-side validation is never optional, even if client-side validation is in place.

In addition to the sensible suggestions you have made with regard to server-side validation, I would add the following:

Just using empty() to test for a blank field is insufficient, because empty() doesn't detect a string of blank spaces. You should always use trim() first to remove any blank spaces from the beginning and end of the input. You can't pass trim() as an argument to empty, so you need to reassign the value first like this:

$_POST['name'] = trim($_POST['name']);

if (!empty($_POST['name'])) {

  // the submission is OK

}