Jump to content

Checking for Posted Data


cyprus

Recommended Posts

I have a form/page with a dropdown list named L1.

The form/page submits back to inself, and when getting re-submitted, I am trying to check for Post data. However I cannot figure out the following senario.

if (isset($_POST['L1'])==true) {
$FT=$_POST['L1'];
echo $FT;
echo "Its Full!";
}

if (isset($_POST['L1'])==false) {
$FT=$_POST['L1'];
echo $FT;
echo "Its Empty!";
}

When the form/page first displays, "Its Empty" is echoed as expected. If I do not click on the dropdown box L1, and just submit the form to itself, I get "Its Full".?

If when the form/page first displays, I click on list L1 and then submit the form back, I get "Its Full" and the value of $FT. However if I repeat the submission, $FT returns empty as would be expected, but the message "Its Full" still appears. What am I missing?  Thanks
Link to comment
Share on other sites

i think your problem is in the use of isset(). isset() tells you whether the variable exists, and in this case $FT will always be true no matter what its' value is after the page is submitted once because the variable is created at that point.

try using a combination of isset() and is_empty(). maybe something like:
[code]
if (isset($_POST['L1']) && !is_empty($_POST['L1'])) {
$FT=$_POST['L1'];
echo $FT;
echo "It's Full. Really it's full!";
}
[/code]

also, you don't need "== true" in your first statement. if you take it out, the if statement by default inquires as to if the statement is true or not, therefore evaluating the same way. and in your second if statement, you could use "!" instead of "== false". they both do the same thing, except this way is a little cleaner to read sometimes - and it looks pretty =)~.
Link to comment
Share on other sites

If at the top of your script, you put
[code]
echo '<pre>', print_r($_POST, true), '</pre>';
[/code]

you will see exactly what was posted.

Firstime you load the page it should show an empty array (no POST data) but when you submit the form all form items (except unchecked checkboxes) are posted, and therefore "set", even if they are empty.
Link to comment
Share on other sites

Many thanks Mr Pancake, that got rid of the error. I used this: echo '<pre>', print_r($_POST, true), '</pre>';
if (isset($_POST['L1']) && empty($_POST['L1'])) {
echo "Its Empty";
} else {
echo "It's Full";
}

However the strange thing is when the page/form first loads, it reports being full. However If I fill $_POST['L1'] with a value from list L1, it behaves normally there after.

However, thanks Barand, your suggestion of putting the code in above, showed:

Array
(
)

So it is not empty to begin with? Is that due to the list L1 getting filled later in the code or is that normal. Can I check for a value of Array() being in $_POST['L1'] or is there another way. Thankyou both
Link to comment
Share on other sites

try:
[code]
if (isset($_POST['L1']) && empty($_POST['L1'])) {
echo "Its Empty";
} else if (isset($_POST['L1']) && !empty($_POST['L1'])) {
echo "It's Full";
}[/code]

or more efficiently as:
[code]
if (isset($_POST['L1']){
  if (empty($_POST['L1'])) {
      echo "Its Empty"; }
  else {
      echo "It's Full";}
else {
  echo "Its Empty";
}
[/code]
Link to comment
Share on other sites

Think I've got it?

if (isset($_POST['L1']) && empty($_POST['L1'])) {
echo "Its Empty";
} else if (isset($_POST['L1']) && !empty($_POST['L1'])) {
echo "It's Full";
}else{
echo "It's really empty from start";
}

Seems to behave itself, fingers crossed. Thanks again to you both for comming in. Regards
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.