473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

refactor + help

There is probably an incredibly simple solution for this problem, but
I can't seem to figure it out. Hopefully one of you gurus can help.

I'm was messing around with PHP today and I wrote a tiny app that asks
for your name and zipcode. If your zipcode is less than 5 characters,
it asks you too enter a zip code with the proper amount of characters.

Here's the code:

<code>
<?php

print <<<_HTML_

<form method='POST' action='$_SERVER[PHP_SELF]'>
Name: <input type='text' name='name'><br /><br />
Zipcode: <input type='text' name='zipcode'><br /><br />
<input type='submit' value='Step 2...'>
</form>
_HTML_;

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

$zip_length = strlen($zipcode);

print $_POST['zipcode'];

if ($zip_length != 5){

print 'invalid amount of characters';

} else {

print ' ';

}

?>

</code>

I cannot figure out how to have the the 'invalid amount of characters'
string post only after the info is passed through the form. Instead it
displays that string on the bottom of the page without any
intervention. I know the answer is sitting right in front of my face.
Maybe someone can help?
Thanks!
Aug 2 '08 #1
12 1334

"silverDuck" <Jo********@gmail.comwrote in message
news:46**********************************@a1g2000h sb.googlegroups.com...
There is probably an incredibly simple solution for this problem, but
I can't seem to figure it out. Hopefully one of you gurus can help.

I'm was messing around with PHP today and I wrote a tiny app that asks
for your name and zipcode. If your zipcode is less than 5 characters,
it asks you too enter a zip code with the proper amount of characters.

Here's the code:

<code>
<?php

print <<<_HTML_

<form method='POST' action='$_SERVER[PHP_SELF]'>
Name: <input type='text' name='name'><br /><br />
Zipcode: <input type='text' name='zipcode'><br /><br />
<input type='submit' value='Step 2...'>
</form>
_HTML_;

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

$zip_length = strlen($zipcode);

print $_POST['zipcode'];

if ($zip_length != 5){

print 'invalid amount of characters';

} else {

print ' ';

}

?>

</code>

I cannot figure out how to have the the 'invalid amount of characters'
string post only after the info is passed through the form. Instead it
displays that string on the bottom of the page without any
intervention. I know the answer is sitting right in front of my face.
Maybe someone can help?
that's because you aren't checking whether or not it was posted in the first
place.

if (isset($_POST['zipcode']) && $zipcode != 5)
{
echo 'INVALID POSTAL CODE - ZIP + 5 FORMAT';
}

also notice the insanity of having an else statement if all you're going to
do is echo a space to the browser!

anyway, hope that helps.
Aug 2 '08 #2
silverDuck wrote:
There is probably an incredibly simple solution for this problem, but
I can't seem to figure it out. Hopefully one of you gurus can help.

I'm was messing around with PHP today and I wrote a tiny app that asks
for your name and zipcode. If your zipcode is less than 5 characters,
it asks you too enter a zip code with the proper amount of characters.

Here's the code:

<code>
<?php

print <<<_HTML_

<form method='POST' action='$_SERVER[PHP_SELF]'>
Name: <input type='text' name='name'><br /><br />
Zipcode: <input type='text' name='zipcode'><br /><br />
<input type='submit' value='Step 2...'>
</form>
_HTML_;

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

$zip_length = strlen($zipcode);

print $_POST['zipcode'];

if ($zip_length != 5){

print 'invalid amount of characters';

} else {

print ' ';

}

?>

</code>

I cannot figure out how to have the the 'invalid amount of characters'
string post only after the info is passed through the form. Instead it
displays that string on the bottom of the page without any
intervention. I know the answer is sitting right in front of my face.
Maybe someone can help?
Thanks!
First of all, remember that all of the PHP code on the page runs BEFORE
the page is sent to the client. So your code will run the first time,
giving the output you see.

To correct your problem, check to see that the form actually was
submitted, i.e.

<?php
if (isset($_POST['submit'] && post['submit'] == 'Step 2')
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';
}
Also, I recommend you move the test earlier in the file, and if you
detect an error, put an error message out right at the field, i.e. just
before the zipcode field. That way the message will be put out just
before error.

Alternatively, put the error message(s) at the beginning of the file
(after any header you might have).

People look for errors at the error location or the top of the window
not at the bottom of the window.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 2 '08 #3
On Aug 1, 8:33*pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
silverDuck wrote:
There is probably an incredibly simple solution for this problem, but
I can't seem to figure it out. Hopefully one of you gurus can help.
I'm was messing around with PHP today and I wrote a tiny app that asks
for your name and zipcode. If your zipcode is less than 5 characters,
it asks you too enter a zip code with the proper amount of characters.
Here's the code:
<code>
<?php
print <<<_HTML_
<form method='POST' action='$_SERVER[PHP_SELF]'>
Name: <input type='text' name='name'><br /><br />
Zipcode: <input type='text' name='zipcode'><br /><br />
<input type='submit' value='Step 2...'>
</form>
_HTML_;
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';
}
?>
</code>
I cannot figure out how to have the the 'invalid amount of characters'
string post only after the info is passed through the form. Instead it
displays that string on the bottom of the page without any
intervention. I know the answer is sitting right in front of my face.
Maybe someone can help?
Thanks!

First of all, remember that all of the PHP code on the page runs BEFORE
the page is sent to the client. *So your code will run the first time,
giving the output you see.

To correct your problem, check to see that the form actually was
submitted, i.e.

<?php
* *if (isset($_POST['submit'] && post['submit'] == 'Step 2')
* * *$zipcode = trim($_POST['zipcode']);
* * *$zip_length = strlen($zipcode);
* * *print $_POST['zipcode'];
* * *if ($zip_length != 5){
* * * *print 'invalid amount of characters';
* * *} else {
* * * *print ' ';

}

Also, I recommend you move the test earlier in the file, and if you
detect an error, put an error message out right at the field, i.e. just
before the zipcode field. *That way the message will be put out just
before error.

Alternatively, put the error message(s) at the beginning of the file
(after any header you might have).

People look for errors at the error location or the top of the window
not at the bottom of the window.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thank you so much! Great help.
Aug 2 '08 #4
silverDuck wrote:
On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>silverDuck wrote:
>>There is probably an incredibly simple solution for this problem, but
I can't seem to figure it out. Hopefully one of you gurus can help.
I'm was messing around with PHP today and I wrote a tiny app that asks
for your name and zipcode. If your zipcode is less than 5 characters,
it asks you too enter a zip code with the proper amount of characters.
Here's the code:
<code>
<?php
print <<<_HTML_
<form method='POST' action='$_SERVER[PHP_SELF]'>
Name: <input type='text' name='name'><br /><br />
Zipcode: <input type='text' name='zipcode'><br /><br />
<input type='submit' value='Step 2...'>
</form>
_HTML_;
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';
}
?>
</code>
I cannot figure out how to have the the 'invalid amount of characters'
string post only after the info is passed through the form. Instead it
displays that string on the bottom of the page without any
intervention. I know the answer is sitting right in front of my face.
Maybe someone can help?
Thanks!
First of all, remember that all of the PHP code on the page runs BEFORE
the page is sent to the client. So your code will run the first time,
giving the output you see.

To correct your problem, check to see that the form actually was
submitted, i.e.

<?php
if (isset($_POST['submit'] && post['submit'] == 'Step 2')
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';

}

Also, I recommend you move the test earlier in the file, and if you
detect an error, put an error message out right at the field, i.e. just
before the zipcode field. That way the message will be put out just
before error.

Alternatively, put the error message(s) at the beginning of the file
(after any header you might have).

People look for errors at the error location or the top of the window
not at the bottom of the window.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Thank you so much! Great help.
Just remember that doing such checks is typically counter productive.
You put in a zipcode check and later you want to include Canada. Oops!

This applies to a whole host of checking that attempts to outsmart the
client.

If you have to do this you should consider doing this client side,
before taking them on a trip to the server and back.

Jeff
Aug 2 '08 #5
Message-ID: <S-******************************@earthlink.comfrom Jeff
contained the following:
Just remember that doing such checks is typically counter productive.
You put in a zipcode check and later you want to include Canada. Oops!

This applies to a whole host of checking that attempts to outsmart the
client.
Heh, a client of mine wanted to make post codes mandatory until I
pointed out that they didn't use them in Ireland...

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk
Aug 2 '08 #6
Jeff wrote:
silverDuck wrote:
>On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>>silverDuck wrote:
There is probably an incredibly simple solution for this problem, but
I can't seem to figure it out. Hopefully one of you gurus can help.
I'm was messing around with PHP today and I wrote a tiny app that asks
for your name and zipcode. If your zipcode is less than 5 characters,
it asks you too enter a zip code with the proper amount of characters.
Here's the code:
<code>
<?php
print <<<_HTML_
<form method='POST' action='$_SERVER[PHP_SELF]'>
Name: <input type='text' name='name'><br /><br />
Zipcode: <input type='text' name='zipcode'><br /><br />
<input type='submit' value='Step 2...'>
</form>
_HTML_;
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';
}
?>
</code>
I cannot figure out how to have the the 'invalid amount of characters'
string post only after the info is passed through the form. Instead it
displays that string on the bottom of the page without any
intervention. I know the answer is sitting right in front of my face.
Maybe someone can help?
Thanks!
First of all, remember that all of the PHP code on the page runs BEFORE
the page is sent to the client. So your code will run the first time,
giving the output you see.

To correct your problem, check to see that the form actually was
submitted, i.e.

<?php
if (isset($_POST['submit'] && post['submit'] == 'Step 2')
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';

}

Also, I recommend you move the test earlier in the file, and if you
detect an error, put an error message out right at the field, i.e. just
before the zipcode field. That way the message will be put out just
before error.

Alternatively, put the error message(s) at the beginning of the file
(after any header you might have).

People look for errors at the error location or the top of the window
not at the bottom of the window.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Thank you so much! Great help.

Just remember that doing such checks is typically counter productive.
You put in a zipcode check and later you want to include Canada. Oops!

This applies to a whole host of checking that attempts to outsmart the
client.

If you have to do this you should consider doing this client side,
before taking them on a trip to the server and back.

Jeff
Even if you validate it client-side you need to validate this
server-side. Never trust what's coming from the client.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 2 '08 #7
Message-ID: <-t******************************@comcast.comfrom Jerry
Stuckle contained the following:
> If you have to do this you should consider doing this client side,
before taking them on a trip to the server and back.

Jeff

Even if you validate it client-side you need to validate this
server-side. Never trust what's coming from the client.
Yes, client side verification is useful for the customer but is no
substitute for server side checking.
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk
Aug 2 '08 #8
On Aug 2, 2:43 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jeff wrote:
silverDuck wrote:
On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
silverDuck wrote:
There is probably an incredibly simple solution for this problem, but
I can't seem to figure it out. Hopefully one of you gurus can help.
I'm was messing around with PHP today and I wrote a tiny app that asks
for your name and zipcode. If your zipcode is less than 5 characters,
it asks you too enter a zip code with the proper amount of characters.
Here's the code:
<code>
<?php
print <<<_HTML_
<form method='POST' action='$_SERVER[PHP_SELF]'>
Name: <input type='text' name='name'><br /><br />
Zipcode: <input type='text' name='zipcode'><br /><br />
<input type='submit' value='Step 2...'>
</form>
_HTML_;
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';
}
?>
</code>
I cannot figure out how to have the the 'invalid amount of characters'
string post only after the info is passed through the form. Instead it
displays that string on the bottom of the page without any
intervention. I know the answer is sitting right in front of my face.
Maybe someone can help?
Thanks!
First of all, remember that all of the PHP code on the page runs BEFORE
the page is sent to the client. So your code will run the first time,
giving the output you see.
>To correct your problem, check to see that the form actually was
submitted, i.e.
><?php
if (isset($_POST['submit'] && post['submit'] == 'Step 2')
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';
>}
>Also, I recommend you move the test earlier in the file, and if you
detect an error, put an error message out right at the field, i.e. just
before the zipcode field. That way the message will be put out just
before error.
>Alternatively, put the error message(s) at the beginning of the file
(after any header you might have).
>People look for errors at the error location or the top of the window
not at the bottom of the window.
>--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thank you so much! Great help.
Just remember that doing such checks is typically counter productive.
You put in a zipcode check and later you want to include Canada. Oops!
This applies to a whole host of checking that attempts to outsmart the
client.
If you have to do this you should consider doing this client side,
before taking them on a trip to the server and back.
Jeff

Even if you validate it client-side you need to validate this
server-side. Never trust what's coming from the client.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
IIRC US zipcodes should be 5 digits AND numeric (its advisable to
validate as extensively as possible, as early as possible) so I'd
suggest using a regular expression rather than just the length of the
string. Further, pregs are mostly the same as javascript regexes - so
the same regex should work at both ends. Something like /^[0-9]{5,5}$/

C.
Aug 4 '08 #9
C. (http://symcbean.blogspot.com/) wrote:
On Aug 2, 2:43 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Jeff wrote:
>>silverDuck wrote:
On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
silverDuck wrote:
>There is probably an incredibly simple solution for this problem, but
>I can't seem to figure it out. Hopefully one of you gurus can help.
>I'm was messing around with PHP today and I wrote a tiny app that asks
>for your name and zipcode. If your zipcode is less than 5 characters,
>it asks you too enter a zip code with the proper amount of characters.
>Here's the code:
><code>
><?php
>print <<<_HTML_
><form method='POST' action='$_SERVER[PHP_SELF]'>
>Name: <input type='text' name='name'><br /><br />
>Zipcode: <input type='text' name='zipcode'><br /><br />
><input type='submit' value='Step 2...'>
></form>
>_HTML_;
>$zipcode = trim($_POST['zipcode']);
>$zip_length = strlen($zipcode);
>print $_POST['zipcode'];
>if ($zip_length != 5){
>print 'invalid amount of characters';
>} else {
>print ' ';
>}
>?>
></code>
>I cannot figure out how to have the the 'invalid amount of characters'
>string post only after the info is passed through the form. Instead it
>displays that string on the bottom of the page without any
>intervention. I know the answer is sitting right in front of my face.
>Maybe someone can help?
>Thanks!
First of all, remember that all of the PHP code on the page runs BEFORE
the page is sent to the client. So your code will run the first time,
giving the output you see.
To correct your problem, check to see that the form actually was
submitted, i.e.
<?php
if (isset($_POST['submit'] && post['submit'] == 'Step 2')
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';
}
Also, I recommend you move the test earlier in the file, and if you
detect an error, put an error message out right at the field, i.e. just
before the zipcode field. That way the message will be put out just
before error.
Alternatively, put the error message(s) at the beginning of the file
(after any header you might have).
People look for errors at the error location or the top of the window
not at the bottom of the window.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thank you so much! Great help.
Just remember that doing such checks is typically counter productive.
You put in a zipcode check and later you want to include Canada. Oops!
This applies to a whole host of checking that attempts to outsmart the
client.
If you have to do this you should consider doing this client side,
before taking them on a trip to the server and back.
Jeff
Even if you validate it client-side you need to validate this
server-side. Never trust what's coming from the client.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

IIRC US zipcodes should be 5 digits AND numeric (its advisable to
validate as extensively as possible, as early as possible) so I'd
suggest using a regular expression rather than just the length of the
string. Further, pregs are mostly the same as javascript regexes - so
the same regex should work at both ends. Something like /^[0-9]{5,5}$/

C.
And what about the 10 character zipcodes, like 12345-6789?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 4 '08 #10
On Aug 4, 4:19 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
C. (http://symcbean.blogspot.com/) wrote:
On Aug 2, 2:43 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jeff wrote:
silverDuck wrote:
On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
silverDuck wrote:
There is probably an incredibly simple solution for this problem, but
I can't seem to figure it out. Hopefully one of you gurus can help.
I'm was messing around with PHP today and I wrote a tiny app that asks
for your name and zipcode. If your zipcode is less than 5 characters,
it asks you too enter a zip code with the proper amount of characters.
Here's the code:
<code>
<?php
print <<<_HTML_
<form method='POST' action='$_SERVER[PHP_SELF]'>
Name: <input type='text' name='name'><br /><br />
Zipcode: <input type='text' name='zipcode'><br /><br />
<input type='submit' value='Step 2...'>
</form>
_HTML_;
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';
}
?>
</code>
I cannot figure out how to have the the 'invalid amount of characters'
string post only after the info is passed through the form. Instead it
displays that string on the bottom of the page without any
intervention. I know the answer is sitting right in front of my face.
Maybe someone can help?
Thanks!
First of all, remember that all of the PHP code on the page runs BEFORE
the page is sent to the client. So your code will run the first time,
giving the output you see.
To correct your problem, check to see that the form actually was
submitted, i.e.
<?php
if (isset($_POST['submit'] && post['submit'] == 'Step 2')
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';
}
Also, I recommend you move the test earlier in the file, and if you
detect an error, put an error message out right at the field, i.e. just
before the zipcode field. That way the message will be put out just
before error.
Alternatively, put the error message(s) at the beginning of the file
(after any header you might have).
People look for errors at the error location or the top of the window
not at the bottom of the window.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thank you so much! Great help.
Just remember that doing such checks is typically counter productive.
You put in a zipcode check and later you want to include Canada. Oops!
This applies to a whole host of checking that attempts to outsmart the
client.
If you have to do this you should consider doing this client side,
before taking them on a trip to the server and back.
Jeff
Even if you validate it client-side you need to validate this
server-side. Never trust what's coming from the client.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
IIRC US zipcodes should be 5 digits AND numeric (its advisable to
validate as extensively as possible, as early as possible) so I'd
suggest using a regular expression rather than just the length of the
string. Further, pregs are mostly the same as javascript regexes - so
the same regex should work at both ends. Something like /^[0-9]{5,5}$/
C.

And what about the 10 character zipcodes, like 12345-6789?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
You can't work out what the regex for that should be?

C.
Aug 6 '08 #11
C. (http://symcbean.blogspot.com/) wrote:
On Aug 4, 4:19 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>C. (http://symcbean.blogspot.com/) wrote:
>>On Aug 2, 2:43 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jeff wrote:
silverDuck wrote:
>On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>>silverDuck wrote:
>>>There is probably an incredibly simple solution for this problem, but
>>>I can't seem to figure it out. Hopefully one of you gurus can help.
>>>I'm was messing around with PHP today and I wrote a tiny app that asks
>>>for your name and zipcode. If your zipcode is less than 5 characters,
>>>it asks you too enter a zip code with the proper amount of characters.
>>>Here's the code:
>>><code>
>>><?php
>>>print <<<_HTML_
>>><form method='POST' action='$_SERVER[PHP_SELF]'>
>>>Name: <input type='text' name='name'><br /><br />
>>>Zipcode: <input type='text' name='zipcode'><br /><br />
>>><input type='submit' value='Step 2...'>
>>></form>
>>>_HTML_;
>>>$zipcode = trim($_POST['zipcode']);
>>>$zip_length = strlen($zipcode);
>>>print $_POST['zipcode'];
>>>if ($zip_length != 5){
>>>print 'invalid amount of characters';
>>>} else {
>>>print ' ';
>>>}
>>>?>
>>></code>
>>>I cannot figure out how to have the the 'invalid amount of characters'
>>>string post only after the info is passed through the form. Instead it
>>>displays that string on the bottom of the page without any
>>>intervention. I know the answer is sitting right in front of my face.
>>>Maybe someone can help?
>>>Thanks!
>>First of all, remember that all of the PHP code on the page runs BEFORE
>>the page is sent to the client. So your code will run the first time,
>>giving the output you see.
>>To correct your problem, check to see that the form actually was
>>submitted, i.e.
>><?php
>> if (isset($_POST['submit'] && post['submit'] == 'Step 2')
>> $zipcode = trim($_POST['zipcode']);
>> $zip_length = strlen($zipcode);
>> print $_POST['zipcode'];
>> if ($zip_length != 5){
>> print 'invalid amount of characters';
>> } else {
>> print ' ';
>>}
>>Also, I recommend you move the test earlier in the file, and if you
>>detect an error, put an error message out right at the field, i.e. just
>>before the zipcode field. That way the message will be put out just
>>before error.
>>Alternatively, put the error message(s) at the beginning of the file
>>(after any header you might have).
>>People look for errors at the error location or the top of the window
>>not at the bottom of the window.
>>--
>>==================
>>Remove the "x" from my email address
>>Jerry Stuckle
>>JDS Computer Training Corp.
>>jstuck...@attglobal.net
>>==================
>Thank you so much! Great help.
Just remember that doing such checks is typically counter productive.
You put in a zipcode check and later you want to include Canada. Oops!
This applies to a whole host of checking that attempts to outsmart the
client.
If you have to do this you should consider doing this client side,
before taking them on a trip to the server and back.
Jeff
Even if you validate it client-side you need to validate this
server-side. Never trust what's coming from the client.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
IIRC US zipcodes should be 5 digits AND numeric (its advisable to
validate as extensively as possible, as early as possible) so I'd
suggest using a regular expression rather than just the length of the
string. Further, pregs are mostly the same as javascript regexes - so
the same regex should work at both ends. Something like /^[0-9]{5,5}$/
C.
And what about the 10 character zipcodes, like 12345-6789?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

You can't work out what the regex for that should be?

C.
Sure I can. But my point is zip codes in the U.S. are not necessarily 5
digits.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 6 '08 #12
Jeff 提到:
silverDuck wrote:
>On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>>silverDuck wrote:
There is probably an incredibly simple solution for this problem, but
I can't seem to figure it out. Hopefully one of you gurus can help.
I'm was messing around with PHP today and I wrote a tiny app that asks
for your name and zipcode. If your zipcode is less than 5 characters,
it asks you too enter a zip code with the proper amount of characters.
Here's the code:
<code>
<?php
print <<<_HTML_
<form method='POST' action='$_SERVER[PHP_SELF]'>
Name: <input type='text' name='name'><br /><br />
Zipcode: <input type='text' name='zipcode'><br /><br />
<input type='submit' value='Step 2...'>
</form>
_HTML_;
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';
}
?>
</code>
I cannot figure out how to have the the 'invalid amount of characters'
string post only after the info is passed through the form. Instead it
displays that string on the bottom of the page without any
intervention. I know the answer is sitting right in front of my face.
Maybe someone can help?
Thanks!
First of all, remember that all of the PHP code on the page runs BEFORE
the page is sent to the client. So your code will run the first time,
giving the output you see.

To correct your problem, check to see that the form actually was
submitted, i.e.

<?php
if (isset($_POST['submit'] && post['submit'] == 'Step 2')
$zipcode = trim($_POST['zipcode']);
$zip_length = strlen($zipcode);
print $_POST['zipcode'];
if ($zip_length != 5){
print 'invalid amount of characters';
} else {
print ' ';

}

Also, I recommend you move the test earlier in the file, and if you
detect an error, put an error message out right at the field, i.e. just
before the zipcode field. That way the message will be put out just
before error.

Alternatively, put the error message(s) at the beginning of the file
(after any header you might have).

People look for errors at the error location or the top of the window
not at the bottom of the window.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Thank you so much! Great help.

Just remember that doing such checks is typically counter productive.
You put in a zipcode check and later you want to include Canada. Oops!

This applies to a whole host of checking that attempts to outsmart the
client.

If you have to do this you should consider doing this client side,
before taking them on a trip to the server and back.

Jeff
IT manager 蛇***眼edmund yau (email擬似ya******@hotmail.com)
全香港最濺IT Manager, 見*著西裝返工. 著得好過佢就問**竟點諗, *小人,
痴撚線
5:00pm俾個job*做, 跟住就自己做到11點搞掂佢, 第2日就小*, 話*搞唔掂, 唔
撚洗放工呀

最撚柒就係呢條友啦, 搞個爛網出黎, 大家去睇下:
網址擬似www.funzy.com

有幾爛? 仆街, 用IE6開會*機架, 真係唔撚知用乜野skill可以寫撚到hang .

成條team朝9晚10, 搞個爛網得個2個function仔, 唔係hang機就係*link, 仲話寫
web要都唔知乜撚野skill set, 食屎啦, 小*生寫個web都唔撚會hang機啦

個個同事想放工都要驚呢樣驚個樣, *張*o的青春濺賣就唔撚好叫人一齊 *濺
賣, 人地都有屋企, *唔撚放工就*o既事, 除撚左打工都唔撚知搞個web未, 屎坑關刀

大家多多張呢個網發佈出去, 聲討呢種小人

Counter
1
Aug 12 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: lawrence | last post by:
Dear Peter, Do we know anyone else who writes PHP code? There is too much work to do, especially if Costin and I are going to join our software together. The easiest way for us to join our...
0
by: Cyanbane | last post by:
Does anyone know if a Demo exists of Refactor! for Vis Studio 2k3 (C#)? It looks like you can order the full version for $99, but I was wondering if you could demo it before. Anyone have any...
1
by: moondaddy | last post by:
I'm running VS 2005 pro vb.net and started to experiment with refactoring. I selected some code, right clicked and selected Refactor / Extract Method. The Refactor popup window appeared. This was...
1
by: PJ | last post by:
Why does the refactor menu not show up in vb.net project while it shows up in c# projects? Thanks ~PJ
12
by: lh84777 | last post by:
Hello, i'm looking for this behaviour and i write a piece of code which works, but it looks odd to me. can someone help me to refactor it ? i would like to walk across a list of items by...
1
by: Steven Nagy | last post by:
Howdy folks. So I have an App from 1.1 that I converted to 2.0. I now want to refactor a bit. One of the things I want to do is create master page for the header, footer and menu that exists on...
2
by: V | last post by:
I've been using VS2005 for a mixed C# / C++ project. Somewhere along the line it seems that 'refactor' on the context menu disappeared for C++ projects. I do believe that this was working at one...
1
by: Andreas Bauer | last post by:
Hi, I have a Data Access Layer where I have to change the method names and signatures. This works fine with the DAL project, but as far as I can see, the refactor function wont update the...
2
by: Smokey Grindel | last post by:
I know in C# under VS2005 there is a nifty "refactor" menu that has a few useful commands that i'd love to have in VB.NET.. anyone know how to get similar functionality in VB.NET 2005? add-ons or...
0
by: =?Utf-8?B?S2luZXRpYyBKdW1wIEFwcGxpZmUgZm9yIC5ORVQg | last post by:
Refactor .NET code using Visual Studio and Refactor! Pro Modifying the existing source code without changing its functionality is called refactoring of Source Code. Visual Studio gives us a lots of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.