Page 1 of 1

switch always seen as Post

Posted: Thu May 13, 2010 11:07 am
by michaelk46
I reused this code from a project I had done in the past, but now I cannot get the script to call secure.html.php. It always sees the switch as set and I am not sure why. I get the feeling I missing something really small. Can anyone see why?

Code: Select all

switch ($_SERVER['REQUEST_METHOD'])
	{
		case 'POST':
			//code to be executed
		
		break;
		default:
			include 'secure.html.php';		
		break;
	}
Here is the form...

Code: Select all

<form action="secure.php" method="post">

Re: switch always seen as Post

Posted: Thu May 13, 2010 11:13 am
by ell0bo
Do you mean that it's always calling the default? That'll happen, unless the request method is actually post.

You might need to explain this issue a little more.

Re: switch always seen as Post

Posted: Thu May 13, 2010 11:14 am
by AbraCadaver
Well if the form method = post then it executes the 'POST' case. Since the include is not in the 'POST' case, why would it include?

Re: switch always seen as Post

Posted: Thu May 13, 2010 11:18 am
by michaelk46
What is happening is whether or not there is information posted from the form, it always executes what's under the 'POST' case in the switch. It won't under any circumstances call secure.html.php which should be called when there is no posted data.
Hope that is a little clearer... sorry for any confusion.

Re: switch always seen as Post

Posted: Thu May 13, 2010 11:43 am
by flying_circus
michaelk46 wrote:What is happening is whether or not there is information posted from the form, it always executes what's under the 'POST' case in the switch. It won't under any circumstances call secure.html.php which should be called when there is no posted data.
Hope that is a little clearer... sorry for any confusion.
It does not matter if there is information inside the form being posted. You are making a post request, regardless. See AbraCadaver's post above.

Re: switch always seen as Post

Posted: Thu May 13, 2010 12:32 pm
by AbraCadaver
flying_circus wrote:
michaelk46 wrote:What is happening is whether or not there is information posted from the form, it always executes what's under the 'POST' case in the switch. It won't under any circumstances call secure.html.php which should be called when there is no posted data.
Hope that is a little clearer... sorry for any confusion.
It does not matter if there is information inside the form being posted. You are making a post request, regardless. See AbraCadaver's post above.
Yes, $_SERVER['REQUEST_METHOD'] will always be post from the form, even if there is no data. You probably want to use something like:

Code: Select all

if(!empty($_POST))
to check if data was posted.

Re: switch always seen as Post

Posted: Thu May 13, 2010 12:47 pm
by ell0bo
Actually, if I read him correct empty($_POST) won't work 100% either.

if ( ( !empty($_POST) && ( !empty($_POST['field1']) || !empty($_POST['field2'])) ) ){
your code
} else
that include

Re: switch always seen as Post

Posted: Thu May 13, 2010 2:15 pm
by michaelk46
Actually, I ended up going with this...

if ($_SERVER['REQUEST_METHOD'] == 'POST') and it's working pretty good.

I saw on another forum that someone thought using if ($_POST) was better than using what is posted above. Is that true? If so why?

Re: switch always seen as Post

Posted: Thu May 13, 2010 3:06 pm
by AbraCadaver
michaelk46 wrote:Actually, I ended up going with this...

if ($_SERVER['REQUEST_METHOD'] == 'POST') and it's working pretty good.

I saw on another forum that someone thought using if ($_POST) was better than using what is posted above. Is that true? If so why?
They are two separate things. If you have a form with method="post", then $_SERVER['REQUEST_METHOD'] will always be equal to 'POST' even if all fields are left blank. I wasn't thinking clearly in my last post, but $_POST will be empty if all fields are empty including the submit button, so it may be of little use because the submit button usually has a value.

It really depends upon what you want to know. Do you want to know if a post operation took place or if all the posted fields are not empty???

Re: switch always seen as Post

Posted: Fri May 14, 2010 8:06 am
by michaelk46
I wanted to know whether or not a post took place but thanks for showing me the if ($_POST). That will help me later with another part of the project I am working on currently.