Re: Confusing POST behavior -- doing it twice?
- From: JDS <jeffrey@xxxxxxxxxxxxxxx>
- Date: Mon, 01 Aug 2005 10:57:05 -0400
On Mon, 01 Aug 2005 13:44:48 +0000, Shelly wrote:
> (posted previously on comp.lang.php but no response received. Cross-posted
> in the dreamweaver forum)
>
> I am confused about what goes on with method POST. Here is an overview of a
> my code, sketching it out:
>
> <?php
> if (isset($_POST['Submit']) && $_POST['Submit']=="Submit") {
> ---Do a bunch of stuff---
> if (isset($_SESSION['Error'])) unset($_SESSION['Error']);
> if (strcmp($A, "any") {
> // Verify that a Zip code is numeric and is five digits
> If (!is_numeric($_POST($zip)) strlen($_POST($zip)) != 5) then {
> $_SESSION['Error'] = "an error description";
> header("Location: thisSite.php:);
> }
> }
> }
> header("Location: anotherSite.php:);
> ?>
>
> ..... in the form description I have an
> <?php if (isset(($_SESSION['Error'])) echo $_SESSION['Error']; ?>
> (Note: The form is type POST).
>
> So here is the problem:
> 1 - When I start, there is no error message.
> 2 - I deliberately put in a bad zip code and have a value other than "any"
> in the A control and click to submit it.
> 3 - Instead of going back to thisSite.php and displaying the error message,
> it goes to anotherSite.php.
> 4 - If I then hit the back arrow on the browser, it displays with the error
> message on thisSite.php
>
> It seems that
> 1 - on the submit it goes through the logic and sets the error.
> 2 - It then must be going through a second time, but this time taking on the
> default setting of "any" for control A and so bypass the logic on the zip
> code and so goes to anotherSite.php.
>
> Please help me here.
>
> Shelly
There are several things that I am not crazy about in your code. And your
usenet post
0) looks like you have MULTI-posted, not CROSS-posted. There is an
important distinction.
1) you may want to consider using "and" instead of "&&". See:
http://www.php.net/manual/en/language.operators.php#language.operators.precedence
(minor issue)
2) Why use strcmp() when == will do? In any case, according to your step
number (2) what you do will not trigger true in "if (strcmp($A, "any") {"
so it will just skip all that stuff and drop out to the final Location
header -- anotherSite.php
3) "When I start, there is no error message" -- when you start what? You
mean, "$_SESSION['Error'] is unset before any of this stuff happens? What
is the error message that appears when you "If I then hit the back arrow
on the browser, it displays with the error"?
4) Your syntax is goofy on a couple of the if() statements.
if (isset($_SESSION['Error'])) unset($_SESSION['Error']);
Why is that whole thing in parens? Is the last paren just before the
semicolon a typo?
If (!is_numeric($_POST($zip)) strlen($_POST($zip)) != 5) then {
How about this one? It *looks* like it has a typo. I can't tell. It looks
like it is missing an "and" or "or" (or && or ||). Are you cutting and
pasting *actual* code into your Usenet post or is this hand typed?
5) The $_SESSION stuff will only show up on sites within the same base
URL. Are "thisSite.php" and "anotherSite.php" on the same server?
6) You are supposed to use absolute URLs when doing a
"header('Location:...')" redirection. Most browsers and servers and PHP
itself are flexible in this regard, but the "official" requirement is the
full URI including "http://".
7) You really should also include "exit;" as the very next line after any
header("Location:..."); -- unexpected results occur otherwise (I learned
the hard way).
8) A cleaner and simpler way (for me) to match five numeric digits is with
regular expressions -- e.g. preg_match(). With PHP's loose data typing, a
numeric thing might actually be a string. In any case, here is how:
preg_match("/\d\d\d\d\d/", $zip);
or
preg_match("/\d{5}/", $zip);
9) One last thing: It would be helpful to see your HTML form as well.
later...
--
JDS | jeffrey@xxxxxxxxxxxxxxx
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/
.
- Follow-Ups:
- Re: Confusing POST behavior -- doing it twice?
- From: Shelly
- Re: Confusing POST behavior -- doing it twice?
- References:
- Confusing POST behavior -- doing it twice?
- From: Shelly
- Confusing POST behavior -- doing it twice?
- Prev by Date: Re: Confusing POST behavior -- doing it twice?
- Next by Date: Re: Gagnez de l'argent sur le net
- Previous by thread: Re: Confusing POST behavior -- doing it twice?
- Next by thread: Re: Confusing POST behavior -- doing it twice?
- Index(es):
Relevant Pages
|