Re: Confusing POST behavior -- doing it twice?



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/

.



Relevant Pages

  • Re: Has anyone ever had this problem?
    ... Uncheck the Enable 3rd party browser extensions ... If you use a HOSTS file, ... error message box comes up immediately upon double-clicking the icon on my ... "An Error has occurred in Internet Explorer. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: RWW: version=1 msgtype=status status=-3 message=Password Inco
    ... As per your request I have attached a screen capture of the error message. ... it is not a browser specific issue. ... Remove registry keys. ...
    (microsoft.public.windows.server.sbs)
  • Re: W3C Validator Errors
    ... Attribute "language" exists, but can not be used ... It's a poorly formulated error message. ... looking in my browser, not the text you are seeing. ... The site is a cms driven site and the syntax is not easy to change. ...
    (alt.html)
  • Re: cant link to pdf pages on external sites
    ... I don't know if it's a Mac versus PC issue, but, the following link: ... Here, however, all .pdf URLs that work in my browser also work fine when links are placed in a document in Word 2007. ... I.e., at what point does it fail, and what error message does it provide? ... But, when trying to click the *same* hyperlink in Word 2007, the error ...
    (microsoft.public.word.application.errors)
  • Re: Silversurfers E-MAIL list IMPORTANT
    ... refused to accept it (the error message is reproduced below). ... Pam is having a problem sending a mail to Froggie using OE in Win. ... bring a blank page onto my browser. ...
    (uk.people.silversurfers)