Click here to get back home

refactor + help

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
refactor + help silverDuck 08-01-2008
Posted by silverDuck on August 1, 2008, 9:19 pm
Please log in for more thread options
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!

Posted by Dale on August 1, 2008, 10:42 pm
Please log in for more thread options

> 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.



Posted by Jerry Stuckle on August 1, 2008, 11:33 pm
Please log in for more thread options
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.
jstucklex@attglobal.net
==================

Posted by silverDuck on August 2, 2008, 12:13 am
Please log in for more thread options
> 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=3D'POST' action=3D'$_SERVER[PHP_SELF]'>
> > Name: <input type=3D'text' name=3D'name'><br /><br />
> > Zipcode: <input type=3D'text' name=3D'zipcode'><br /><br />
> > <input type=3D'submit' value=3D'Step 2...'>
> > </form>
> > _HTML_;
>
> > $zipcode =3D trim($_POST['zipcode']);
>
> > $zip_length =3D strlen($zipcode);
>
> > print $_POST['zipcode'];
>
> > if ($zip_length !=3D 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. =A0So 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
> =A0 =A0if (isset($_POST['submit'] && post['submit'] =3D=3D 'Step 2')
> =A0 =A0 =A0$zipcode =3D trim($_POST['zipcode']);
> =A0 =A0 =A0$zip_length =3D strlen($zipcode);
> =A0 =A0 =A0print $_POST['zipcode'];
> =A0 =A0 =A0if ($zip_length !=3D 5){
> =A0 =A0 =A0 =A0print 'invalid amount of characters';
> =A0 =A0 =A0} else {
> =A0 =A0 =A0 =A0print ' ';
>
> }
>
> 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. =A0That 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.
>
> --
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Thank you so much! Great help.

Posted by Jeff on August 2, 2008, 3:51 am
Please log in for more thread options
silverDuck wrote:
>> 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


Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap