HighDots Forums  

Confusing POST behavior

Macromedia Dreamweaver Macromedia Dreamweaver Discussions (macromedia.dreamweaver)


Discuss Confusing POST behavior in the Macromedia Dreamweaver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
sheldonlg
 
Posts: n/a

Default Confusing POST behavior - 08-01-2005 , 09:41 AM






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

(posted previously on comp.lang.php but no response received. I am going to
repost it there as well)


Reply With Quote
  #2  
Old   
Michael Fesser
 
Posts: n/a

Default Re: Confusing POST behavior - 08-01-2005 , 10:07 AM






.oO(sheldonlg)

Quote:
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") {
Where does $A come from? There's no value assigned to it in the code
above. Additionally there's a ')' missing.

Quote:
// Verify that a Zip code is numeric and is five digits
If (!is_numeric($_POST($zip)) strlen($_POST($zip)) != 5) then {
Operator missing (probably '||').

Quote:
$_SESSION['Error'] = "an error description";
header("Location: thisSite.php;
The Location-header requires an absolute URL.

Quote:
header("Location: anotherSite.php;
Same here.

Micha


Reply With Quote
  #3  
Old   
sheldonlg
 
Posts: n/a

Default Re: Confusing POST behavior - 08-01-2005 , 10:45 AM



Michael Fesser wrote:
Where does $A come from? There's no value assigned to it in the code
above. Additionally there's a ')' missing.

Me:
A is a control that is a list box that has distances. It the value is "any".
an additional control appears that has the name "ziip". There were MANY errors
in what I typed in the post (that are not in the actual full code). I have
included a corrected copy at the end of this message.

Michael:
The Location-header requires an absolute URL.

Me:
Not true. I have been using relative URLs everywhere in my code and they work
perfectly.

Corrected code:
<?php
if (isset($_POST['Submit']) && $_POST['Submit']=="Submit") {
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).




Reply With Quote
  #4  
Old   
sheldonlg
 
Posts: n/a

Default Re: Confusing POST behavior - 08-01-2005 , 11:09 AM



I don't understand it, but I solved my immediate problem. If I put a line

exit();

after the
header("Location: thisSite.php");

line, then it works as I want.

Someone please explain this to me.

Shelly


Reply With Quote
  #5  
Old   
Michael Fesser
 
Posts: n/a

Default Re: Confusing POST behavior - 08-01-2005 , 11:27 AM



.oO(sheldonlg)

Quote:
I don't understand it, but I solved my immediate problem. If I put a line

exit();

after the
header("Location: thisSite.php");

line, then it works as I want.

Someone please explain this to me.
When invoking the header() call the script doesn't stop immediately, but
it continues to run, which may lead to unexpected results as you have
seen. All headers are "collected" and sent out at the end of the script.

So when doing a redirect there must be an exit() call after the
Location-header to be sure the script really stops there. Sorry, I
overlooked that in my first reply.

But you should still fix the other errors in your code, even if they
were not the reason for the current problem. They might lead to other
problems.

Micha


Reply With Quote
  #6  
Old   
Michael Fesser
 
Posts: n/a

Default Re: Confusing POST behavior - 08-01-2005 , 11:27 AM



.oO(sheldonlg)

Quote:
Michael Fesser wrote:
Where does $A come from? There's no value assigned to it in the code
above. Additionally there's a ')' missing.

Me:
A is a control that is a list box that has distances. It the value is "any".
an additional control appears that has the name "ziip".
Then it should be $_POST['A'] instead of just $A. On a recent system $A
would be undefined and cause a notice. Check if error_reporting is set
to E_ALL in your php.ini, on a development system that is a must.

Quote:
Michael:
The Location-header requires an absolute URL.

Me:
Not true. I have been using relative URLs everywhere in my code and they work
perfectly.
Doesn't matter. The full URL is _required_ by the HTTP spec. Just
because it works with your browsers doesn't mean it will work in
general. There are all kinds of different user agents (browsers, search
engine spiders, proxys etc.), which fetch data from a server using HTTP.
You don't know if your wrong code will work with them all. So better fix
it.

In the PHP manual you can find an example how to automatically build an
absolute URL from a relative one using predefined server variables.

http://www.php.net/header

Micha


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.