...

View Full Version : Checking for submitted form



thesavior
03-28-2010, 01:00 PM
I'm trying to figure out what the best method is to check if a form has been submitted. The techniques I'm aware of are the following:

1) Submit to a separate page, that page always assumes a form submission
2) Store a hidden input and check for that hidden input at process time
3) Using ($_SERVER['REQUEST_METHOD'] == "POST")

They all have their pros and cons, but I'm interested in how people feel about the different solutions. I've used #2 for years, but have recently started migrating over to #3. What bothers me about #3 is that the page can only run on one form easily (obviously you could just add a couple if statements), where as with #2, you could give each form it's own unique hidden input, and there would be no problems.

DJCMBear
03-28-2010, 01:40 PM
Why dont you name the input fields for there job such as for a login have an input name for the username as "login_user" and the use a php if statement to check for it like this:


if(isset($_POST['login_user']) && $_POST['login_user']!=""){
// Your code here
}

That way you can call different post calls at different time and not when you use $_SERVER['request_method']=="POST".

You may want to use some php validation methods aswell such as is_numeric if you want all numbers and no text.

masterofollies
03-28-2010, 03:22 PM
I have always used #1. Just use extract($_POST) and then echo out the form.

DJCMBear
03-28-2010, 09:47 PM
I have always used #1. Just use extract($_POST) and then echo out the form.

You may not use one file for all posted data but like i said you cant submit a post without php codes that define the job being processed such as $_POST['hello'] and $_POST['bye'] are both a post object so if you say if(isset($_POST)) or if($_SERVER['request_method']=="POST") they could mean anything so both hello and bye functions would be called. Thats why i said define a name for the job at hand so your post querys can be used easerly, such as login_user for the input username where the user logs in, and then use if(isset($_POST) && $_POST['login_user']!="").

masterofollies
03-28-2010, 11:22 PM
I was simply saying what I use in my scripts.

DJCMBear
03-29-2010, 12:52 AM
i would suggest using #3 for submitting a form but use the way i said otherwise you would need alot of if/if else statements.

MattF
03-29-2010, 01:06 AM
i would suggest using #3 for submitting a form but use the way i said otherwise you would need alot of if/if else statements.

There's absolutely no need to check the SERVER var. You're already doing the relevant check for POST with this code:



if (isset($_POST) && $_POST['login_user']!="")



Edit: I'd personally change that to:



if (isset($_POST['login_user']) && !empty($_POST['login_user']))

DJCMBear
03-29-2010, 02:13 AM
if (isset($_POST['login_user']) && !empty($_POST['login_user']))


Thats a good way to go because this way you can have more than one form with the method post and you can call certain codes when you need it unlike your first post when you said about $_SERVER['REQUEST_METHOD']=="POST" which would call anything between that if statement, so you are now headding into the right direction with the php post code.

MattF
03-29-2010, 02:19 AM
unlike your first post[...snipped]so you are now heading into the right direction with the php post code.

Wrong person. I'm not the O.P. :D

DJCMBear
03-29-2010, 02:24 AM
lol sorry wasnt paying attention to posters name haha

and from your post you say theres no need to check for server var which is why i said not to use $_SERVER['REQUEST_METHOD']

MattF
03-29-2010, 02:31 AM
lol sorry wasnt paying attention to posters name haha

No worries. :D



and from your post you say theres no need to check for server var which is why i said not to use $_SERVER['REQUEST_METHOD']

Aye, it is fairly moot checking it. If one of the known POST vars for the particular form is set, it's a fairly safe assumption that the form has indeed been posted.

DJCMBear
03-29-2010, 02:33 AM
I normaly check for one of the required fields aswell as the form thats being checked so that it will only call the codes needed if all prams are met.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum