Jump to content


Photo

Checking for Posted Data


  • Please log in to reply
9 replies to this topic

#1 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 17 September 2006 - 09:32 PM

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

#2 Mr_Pancakes

Mr_Pancakes
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationSouthwest USA

Posted 17 September 2006 - 09:50 PM

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:
if (isset($_POST['L1']) && !is_empty($_POST['L1'])) {
$FT=$_POST['L1'];
echo $FT;
echo "It's Full. Really it's full!";
}

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 =)~.

#3 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 17 September 2006 - 10:06 PM

Many thanks, however it's unhappy with the function is_empty, fatal error??

#4 Mr_Pancakes

Mr_Pancakes
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationSouthwest USA

Posted 17 September 2006 - 10:08 PM

opps, you're right - try just empty() instead,. (ive gotten used to using some custom functions of my own)

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 17 September 2006 - 10:11 PM

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

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 17 September 2006 - 10:29 PM

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

#7 Mr_Pancakes

Mr_Pancakes
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationSouthwest USA

Posted 17 September 2006 - 10:47 PM

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

or more efficiently as:
if (isset($_POST['L1']){
   if (empty($_POST['L1'])) {
      echo "Its Empty"; }
   else {
      echo "It's Full";}
else {
   echo "Its Empty";
}


#8 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 17 September 2006 - 11:07 PM

Thanks again. The second code has a problem, but the first one works. On first page load it does not report any message but in use does the correct thing. I will have a play to see how I can get a first loaded page message of empty. Thanks for your help.

#9 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 17 September 2006 - 11:16 PM

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

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 17 September 2006 - 11:28 PM

Logically, that code looks exactly the same as Mr Pancakes second.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users