cyprus Posted September 17, 2006 Share Posted September 17, 2006 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 Quote Link to comment Share on other sites More sharing options...
Mr_Pancakes Posted September 17, 2006 Share Posted September 17, 2006 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 =)~. Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 17, 2006 Author Share Posted September 17, 2006 Many thanks, however it's unhappy with the function is_empty, fatal error?? Quote Link to comment Share on other sites More sharing options...
Mr_Pancakes Posted September 17, 2006 Share Posted September 17, 2006 opps, you're right - try just empty() instead,. (ive gotten used to using some custom functions of my own) Quote Link to comment Share on other sites More sharing options...
Barand Posted September 17, 2006 Share Posted September 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 17, 2006 Author Share Posted September 17, 2006 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 Quote Link to comment Share on other sites More sharing options...
Mr_Pancakes Posted September 17, 2006 Share Posted September 17, 2006 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] Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 17, 2006 Author Share Posted September 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 17, 2006 Author Share Posted September 17, 2006 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted September 17, 2006 Share Posted September 17, 2006 Logically, that code looks exactly the same as Mr Pancakes second. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.