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