...

View Full Version : PHP $_POST problems



braedonvogel
02-14-2008, 02:31 AM
My PHP code relies very heavily on $_POST variable data. It generates a form which when submitted, reloads the same PHP page according to what $_POST data the user has entered. The problem is it needs the data to know what to generate, and when you visit the page for the first time not having submitted data, it doesn't display what it should. Is there any way I can have default values for my $_POST variables so it will show correctly the first time you visit? I've already tried the following and they don't seem to work...

$spec = $_POST["spec"];
if ($spec = Null) {$spec = "";}

or

$spec = $_POST["spec"];
if ($spec = Undefined) {$spec = "";}

Fumigator
02-14-2008, 04:50 AM
Use the isset() function. You should only have to test one $_POST variable, since if one is set, it's safe to assume the form has been submitted and they're all set.



if (!isset($_POST['submitBtn'])) {
//do the stuff you'd do the first time on the page
} else {
//do the stuff you'd do once the form has been submitted
}

Anwar ul Haq
02-14-2008, 07:51 AM
yeah... isset will do :)

rafiki
02-14-2008, 02:42 PM
or empty()?

hammer65
02-14-2008, 04:00 PM
empty will handle if the variable is not set and if it is blank. Watch out for values that can validly be 0 however. that also evaluates to empty. You could also check the request_method to determine if it was a post.

aedrin
02-14-2008, 04:39 PM
or empty()?

Don't use empty(). If you do, you are automatically saying that all fields are required. In some cases this might work, but it's just silly to use this. Just use isset().

hammer65
02-14-2008, 04:48 PM
Don't use empty(). If you do, you are automatically saying that all fields are required. In some cases this might work, but it's just silly to use this. Just use isset().

If you use empty on the entire $_POST variable, it will return true if $_POST is not set, is null or if there are no elements in the post array. If you use it on a single value it will return true if the varaible is not set, null or if it is an empty string or 0. It will not return true if you have a 20 element array where each element is blank. That doesn't constitute all fields required.

http://us3.php.net/empty

aedrin
02-14-2008, 04:50 PM
Who suggested that it should be used like that?

hammer65
02-14-2008, 05:07 PM
Who suggested that it should be used like that?



Don't use empty(). If you do, you are automatically saying that all fields are required.


That statement is incorrect. How can the use of empty in this way...



if (empty($_POST['submitBtn'])) {
//do the stuff you'd do the first time on the page
} else {
//do the stuff you'd do once the form has been submitted
}
require all fields? The OP simply wants to know if the request is post or not. He wasn't asking about validation.

aedrin
02-14-2008, 05:18 PM
The OP simply wants to know if the request is post or not. He wasn't asking about validation.

Correct.

Hence using empty() would impose another requirement (that the field have content when submitted) on the field. Which is (as you said) not what the OP requested. Besides the issue that 0 is also considered empty (it's not hard to imagine a form that requires 0 as its input).


require all fields?

Obviously I am not implying that using it once will require all fields. If the OP would have replaced all isset() calls with empty(), then all fields would be required.

Not sure how my statement is incorrect in that sense.

hammer65
02-14-2008, 05:29 PM
Correct.

Hence using empty() would impose another requirement (that the field have content when submitted) on the field. Which is (as you said) not what the OP requested. Besides the issue that 0 is also considered empty (it's not hard to imagine a form that requires 0 as its input).



Obviously I am not implying that using it once will require all fields. If the OP would have replaced all isset() calls with empty(), then all fields would be required.

Not sure how my statement is incorrect in that sense.

You did notice that the value being checked is called, "submitBtn" did you not? Most of the time you just see.



if($_POST['submit'])
{
// we have a form submission because the button value is there
}
The value of the submit button should be known. If the value isn't what is expected then someone is monkeying with the form.

aedrin
02-14-2008, 05:37 PM
You did notice that the value being checked is called, "submitBtn" did you not?

No, I did not. I didn't think someone would have 2 versions of a form to cover the initial load. The initial example didn't really suggest what was done in either branch though. So that actually makes it more confusing of an answer.


Most of the time you just see.

Not sure what you mean by this though.

hammer65
02-14-2008, 06:08 PM
Not sure what you mean by this though.

I normally don't use the method but most of the time I have seen that method used to detect if the form is being submitted or if it is first visit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum