switch always seen as Post

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

switch always seen as Post

Post 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">
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: switch always seen as Post

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: switch always seen as Post

Post 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?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: switch always seen as Post

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: switch always seen as Post

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: switch always seen as Post

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: switch always seen as Post

Post 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
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: switch always seen as Post

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: switch always seen as Post

Post 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???
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: switch always seen as Post

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