Help - Search - Member List - Calendar
Full Version: A PHP question
WorkTheWeb Forums > Webmaster Resources > Webmaster - General Help
Support our Sponsors!
Si
Hi all.

I have been tasked with building a contact form in PHP.

I am having a few troubles with form validation. I have Googled around
and the scripts and help I have found have not been of much use.

The contact form posts to itself and detects if the submit has been
pressed or not.

Sample code (Sorry, page not published anywhere yet.)

<?php

$Name=$_GET['Name'];
$AltPhone=$_GET['AltPhone'];
......etc
......etc
......etc

$Submit=$_GET['Submit'];

if (!isset($_GET['Submit'])) {
?>

Form in here

<?
}
else {
mail( "[Email Removed]", "Feedback Form Results", "Name : $Name " );
header( "Location: http://yy.com/thankyou.html" );
}
?>

Can anyone point me the right direction for validating the following:

This is how I would do it in ASP.

if len(AltPhone)<12 then
##FAIL##
else
##PASS##
end if

Only if all form fields containe something, post the email.

Any help or pointers would be greatly appreciated, strange how this
would take me minutes to write in ASP and has had me running round in
Google circles for ages now.

Thanks

Si

Dylan Parry
Using a pointed stick and pebbles, Si scraped:

QUOTE
if len(AltPhone)<12 then
##FAIL##
else
##PASS##
end if

if (AltPhone.length() < 12) {
// Fail
}
else {
// Pass
}

--
Dylan Parry
http://webpageworkshop.co.uk -- FREE Web tutorials and references

Si
Dylan Parry wrote:
QUOTE
Using a pointed stick and pebbles, Si scraped:

if len(AltPhone)<12 then
##FAIL##
else
##PASS##
end if


if (AltPhone.length() < 12) {
// Fail
}
else {
// Pass
}

Many thanks, I shall have a go and see if I can get this thing working

Si

Chris Hope
Dylan Parry wrote:

QUOTE
Using a pointed stick and pebbles, Si scraped:

if len(AltPhone)<12 then
##FAIL##
else
##PASS##
end if

if (AltPhone.length() < 12) {
// Fail
}
else {
// Pass
}


It's not like that in PHP. It's like this:

if (strlen($AltPhone) < 12) {
// fail
}
else {
// pass
}

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

Dylan Parry
Using a pointed stick and pebbles, Chris Hope scraped:

QUOTE
It's not like that in PHP. It's like this:
if (strlen($AltPhone) < 12) {

Oops :) You're right of course. I've got my C# hat on today ;) In fact,
last weekend was the first time in months that I actually wrote any PHP
code, so I guess I am a bit (read "very") rusty :(

--
Dylan Parry
http://electricfreedom.org -- Where the Music Progressively Rocks!

Dylan Parry
Using a pointed stick and pebbles, Si scraped:

QUOTE
Many thanks, I shall have a go and see if I can get this thing working

You won't have any chances there, sorry. I was being as thick as a brick
when I wrote that code! What Chris wrote is more likely to work ;)

--
Dylan Parry
http://webpageworkshop.co.uk -- FREE Web tutorials and references

rf
Dylan Parry
QUOTE

Oops :) You're right of course. I've got my C# hat on today ;) In fact,
last weekend was the first time in months that I actually wrote any PHP
code, so I guess I am a bit (read "very") rusty :(

What can be even worse is if you forget to take off your PHP hat when
returning to C#, or in my case C++. It does work but is *not* what you
intended.

CString s = ...

if (strlen(s) ... // a time consuming search through the typecast char array
looking for a 0x00

if (s.GetLength() ... // a direct access to the s's internal length member

You should colour code your hats. C++ is a nice bright blue. C# is canary
yellow. PHP is a dull muddy brown.


However I wonder why Si is insisting that what looks like a phone number be
greater than 12 characters long. They may be in the UK but what if *my*
phone number is less than 12 numbers?

It reminds me of when I was in California skiing. They were doing some
domographics at the ticket counter, asking where one lives etc. Well, I live
in Australia, state NSW, postcode 2154.

There was *no* field for country. NSW was *not* one of the available 51
states. The "zip" code *must* be 5 digits long. All skiers at Mammoth
Mountain *must* presumably live in the U S of A.

Needless to say we had to lie to obtain our day pass, we all live in the ski
lodge down in the village. So much for their demographics.

Be very carefull when you validate somebodys personal details.

Cheers
Richard.

Si
Chris Hope wrote:
QUOTE
Dylan Parry wrote:

if len(AltPhone)<12 then
##FAIL##
else
##PASS##
end if

if (strlen($AltPhone) < 12) {
// fail
}
else {
// pass
}


Thanks for the above, it's cracked most of it for me.

I have been asked now to validate that the tel number starts with a '0',
how do I go about this in PHP, again, dead easy in ASP but I was thrown
this as a PHP project :-( Oh well, we all have to learn.

BTW, I think I have some un-necessary ifs and elses in this script now
so I shall post it up soon to see how much can be trimmed. Seems and
awful lot of code for a form script. Probably my cobbling things
together that's doing it.

GreyWyvern
And lo, Si didst speak in alt.www.webmaster:

QUOTE
Thanks for the above, it's cracked most of it for me.

I have been asked now to validate that the tel number starts with a '0',

if ($AltPhone{0} === "0") {
...
}


Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/ringmaker - Orca Ringmaker: Host a web ring
from your website!

Si
GreyWyvern wrote:
QUOTE
And lo, Si didst speak in alt.www.webmaster:

I have been asked now to validate that the tel number starts with a '0',


if ($AltPhone{0} === "0") {
...
}

Thanks Grey

The above didn't actually trap the lack of a starting 0, but seeing how
you had structed the statement I changed it to ($AltPhone{0} >0) and it
worked.

Only a minor CSS issue to solve now then I think thats it

Si

William Tasso
Writing in news:alt.www.webmaster
From the safety of the cafeteria
Si <[Email Removed]> said:

QUOTE
...
I am having a few troubles with form validation....

I can see the technical answer has been given elsewhere in this thread so
please indulge a little drifting ...

Not having sight of your completed form I don't know if this is relevant
but, from my perspective form validation (on the typical contact-us page)
is usually an unnecessary task. Most such forms have prompts for more
than one channel of communication - does the site owner/manager really
want to turn away punters that prefer to do business via email, fax or
even land-mail?

--
William Tasso

Beauregard T. Shagnasty
Si wrote:
QUOTE
Hi all.

Hi.

And in addition to all the other stuff, why are you using GET
variables instead of POST?

QUOTE
$Name=$_GET['Name'];
$AltPhone=$_GET['AltPhone'];
.....etc
.....etc
.....etc

$Submit=$_GET['Submit'];

if (!isset($_GET['Submit'])) {

--
-bts
-This space intentionally left blank.

Si
William Tasso wrote:
QUOTE
Writing in news:alt.www.webmaster
From the safety of the  cafeteria
Si <[Email Removed]> said:

...
I am having a few troubles with form validation....


I can see the technical answer has been given elsewhere in this thread
so  please indulge a little drifting ...

Not having sight of your completed form I don't know if this is
relevant  but, from my perspective form validation (on the typical
contact-us page)  is usually an unnecessary task.  Most such forms have
prompts for more  than one channel of communication - does the site
owner/manager really  want to turn away punters that prefer to do
business via email, fax or  even land-mail?

I completely agree, from my point of view, a user has started to
complete a form because they *want* to communicate with the
people/business behind the website. To this end, to force them to
provide information they mat not be comfortable divulging may result in
them surfing elsewhere, potential customer lost!

I usually validate against names being empty along with the comments
field, these would probably be completed anyway. I usually request
confirmation of email address as well to try to prevent typos, I have
had a couple of instances when a form has been sent to me, but the email
address has been wrongly submitted and no telephone number was sent so I
had no reply path. Seemed geniune contact as well. One person did get in
touch a second time the other never did, probably thought I was being
lazy by not replying.

The form I am actually building today, hence my PHP questions, is more
like a mini questionaire so most of the fields are radio or text
buttons. Unfortunately, the client has requested that 2 different
telephone numbers are provided and data is verfied for each field, *very
dangerous imho*. As I am actually doing this as a sub-contract for the
design agency who has the client, I cannot do much other than warn of
the implications of the second number validation.

Si

Si
Beauregard T. Shagnasty wrote:
QUOTE
And in addition to all the other stuff, why are you using GET variables
instead of POST?

Using POST now in almost finished version, was just using GET to make
sure the form was submitting. Nothing seemed to be happening at first.


QUOTE
$Name=$_GET['Name'];
$AltPhone=$_GET['AltPhone'];
.....etc
.....etc
.....etc

$Submit=$_GET['Submit'];

if (!isset($_GET['Submit'])) {


MGW
On Wed, 06 Jul 2005 11:05:41 GMT, "rf" <@invalid.com> wrote:

QUOTE
It reminds me of when I was in California skiing. They were doing some
domographics at the ticket counter, asking where one lives etc. Well, I live
in Australia, state NSW, postcode 2154.

There was *no* field for country. NSW was *not* one of the available 51
states. The "zip" code *must* be 5 digits long. All skiers at Mammoth
Mountain *must* presumably live in the U S of A.

Having skied at Mammoth, I can't see why anyone would travel from
outside California to ski there, let alone outside the US ;-P

--

MGW

Tony
Si wrote:
QUOTE
Thanks for the above, it's cracked most of it for me.

I have been asked now to validate that the tel number starts with a
'0', how do I go about this in PHP, again, dead easy in ASP but I was
thrown this as a PHP project :-( Oh well, we all have to learn.

If you haven't done so already, you should probably take a good look at the
PHP manual at http://us3.php.net/manual/en/index.php (for English) - I'm
pretty decent with PHP and I use it quite a lot.

For the specific instance, try:

if (substr($phone_number, 0, 1) == '0') {
// PASS
} else {
// FAIL
}

--
Tony Garcia
Web Right! Development

Si
Si wrote:
QUOTE

Any help or pointers would be greatly appreciated, strange how this
would take me minutes to write in ASP and has had me running round in
Google circles for ages now.

Many thanks to those who helped me build this page. Esp Grey and Chris.

I now have a completed form with error messages working well.

The code seems a bit bloated and if it where ASP I would have a page
about 50% the size of this one, I would like you good people to point
out where it can be trimmed.

This would be an exercise for myself, I am classing this page as
completed, initially I was only asked to do the html anyway! It is now
on the graphic designers site so can't post the URL until release.

I don't have a PHP enabled server to publish this page onto myself, so,
if anyone is interested/inclined to see <cover my back>[the hash i made
of (possible)]</cover my back> this script, shall I post the code
complete here or place it as a .txt file on my server for someone to
upload to there own and let all have a look at front and back end?

Si

Si
Jerry Stuckle wrote:
QUOTE
Si wrote:

GreyWyvern wrote:

And lo, Si didst speak in alt.www.webmaster:

I have been asked now to validate that the tel number starts with a
'0',

if ($AltPhone{0} === "0") {
...
}



Thanks Grey

The above didn't actually trap the lack of a starting 0, but seeing
how you had structed the statement I changed it to ($AltPhone{0} >0)
and it worked.


Si,

Be careful.

OK, got me listening :-)

QUOTE

$AltPhone='abcdef';
if ($AltPhone{0} > 0) ... is FALSE.

In a numeric comparison (which you have), non-digits are converted to
0.  So, as a numeric comparison you're actually comparing:

if ( 0 > 0 )

I did wonder what would happen if someone typed in +44 etc etc but
didn't actually get round to testing that in the end [being pressured
for completion! grr]

QUOTE

Grey's use is best - it compares the type as well as the value.  Since
you want the opposite, use

if ($AltPhone{0} !== '0')  // Note the quotes and comparison of type
and value

I have changed the script to above and it worked, but Grey's didn't, is
it down to the ' as opposed to " in Grey's?

QUOTE
or

if (!($AltPhone{0} === '0')) // Same as above


Thanks for this

Jerry Stuckle
Si wrote:
QUOTE
GreyWyvern wrote:

And lo, Si didst speak in alt.www.webmaster:


I have been asked now to validate that the tel number starts with a '0',



if ($AltPhone{0} === "0") {
...
}


Thanks Grey

The above didn't actually trap the lack of a starting 0, but seeing how
you had structed the statement I changed it to ($AltPhone{0} >0) and it
worked.

Only a minor CSS issue to solve now then I think thats it

Si

Si,

Be careful.

$AltPhone='abcdef';
if ($AltPhone{0} > 0) ... is FALSE.

In a numeric comparison (which you have), non-digits are converted to 0. So, as
a numeric comparison you're actually comparing:

if ( 0 > 0 )

Grey's use is best - it compares the type as well as the value. Since you want
the opposite, use

if ($AltPhone{0} !== '0') // Note the quotes and comparison of type and value

or

if (!($AltPhone{0} === '0')) // Same as above

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
[Email Removed]
==================

Tony
Jerry Stuckle wrote:
QUOTE
Be careful.

$AltPhone='abcdef';
if ($AltPhone{0} > 0) ... is FALSE.

In a numeric comparison (which you have), non-digits are converted to
0.  So, as a numeric comparison you're actually comparing:

if ( 0 > 0 )

Grey's use is best - it compares the type as well as the value. Since you
want the opposite, use

if ($AltPhone{0} !== '0')  // Note the quotes and comparison of
type and value

if ($AltPhone{0} != '0') // only one equal sign :)

QUOTE

or

if (!($AltPhone{0} === '0')) // Same as above

--
Tony Garcia
Web Right! Development
Riverside, CA
www.WebRightDevelopment.com

Jerry Stuckle
Si wrote:
QUOTE
Jerry Stuckle wrote:

Si wrote:

GreyWyvern wrote:

And lo, Si didst speak in alt.www.webmaster:


I have been asked now to validate that the tel number starts with a
'0',


if ($AltPhone{0} === "0") {
...
}




Thanks Grey

The above didn't actually trap the lack of a starting 0, but seeing
how you had structed the statement I changed it to ($AltPhone{0} >0)
and it worked.


Si,

Be careful.


OK, got me listening :-)


$AltPhone='abcdef';
if ($AltPhone{0} > 0) ... is FALSE.

In a numeric comparison (which you have), non-digits are converted to
0.  So, as a numeric comparison you're actually comparing:

if ( 0 > 0 )


I did wonder what would happen if someone typed in +44 etc etc but
didn't actually get round to testing that in the end [being pressured
for completion! grr]


Grey's use is best - it compares the type as well as the value.  Since
you want the opposite, use

if ($AltPhone{0} !== '0')  // Note the quotes and comparison of type
and value


I have changed the script to above and it worked, but Grey's didn't, is
it down to the ' as opposed to " in Grey's?

or

if (!($AltPhone{0} === '0')) // Same as above


Thanks for this

In some ways, yes. But mainly it's the use of !== or ! ... === ... comparisons.

=== and !== compare type as well as value. == and != only compare value (after
conversion). The ! before the expression just inverts the results of the test.

And comparing to 0 compares to numeric value zero. Comparing to '0' compares to
the character zero. A small but very important difference.




--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
[Email Removed]
==================

Dylan Parry
Using a pointed stick and pebbles, Tony scraped:

QUOTE
if ($AltPhone{0} != '0') // only one equal sign :)

Two equal signs is better as it means "exactly equal to" - in other
words it compares both the value and the type:

true == 1
false == 0

but,

true !== 1
false !== 0

--
Dylan Parry
http://electricfreedom.org -- Where the Music Progressively Rocks!

Tony
Dylan Parry wrote:
QUOTE
Using a pointed stick and pebbles, Tony scraped:

if ($AltPhone{0} != '0') // only one equal sign :)

Two equal signs is better as it means "exactly equal to"  - in other
words it compares both the value and the type:

true == 1
false == 0

but,

true !== 1
false !== 0

Ah - I see what you're doing. I wasn't aware of that particular expression -
thought it was a typo :)

Just shows that there's always something to learn...

--
Tony Garcia
Web Right! Development
Riverside, CA
www.WebRightDevelopment.com


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.