msebar Posted October 8, 2014 Share Posted October 8, 2014 (edited) Sorry for the miss spell on correctly counldn't edit the title I need to have the following 3 varibles checked to see if they are set and if not display an error saying "All fields need to be filled in."My problem is that it's not working. Did I mess up on my if statement?$tempneeded$lowtemp$gallons <?php $tempneeded = $_POST['tempneeded']; $lowtemp = $_POST['lowtemp']; $gallons = $_POST['gallons']; htmlspecialchars($tempneeded, ENT_QUOTES, 'UTF-8') . ' ' . htmlspecialchars($lowtemp, ENT_QUOTES, 'UTF-8') . ' ' . htmlspecialchars($gallons, ENT_QUOTES, 'UTF-8') . ' ' . $wgd = .5; $tempdiff = $tempneeded - $lowtemp; $wattsneeded = $tempdiff * ($gallons * $wgd); if (!isset ($lowtemp,$tempneeded,$gallons)) { echo "<b>All fields need to be filled in.</b>"; } else { echo "<b>You will need a total of $wattsneeded watts to rasie your tempature $tempdiff degress, from $lowtemp to $tempneeded.</b></td></tr>"; } ?> Edited October 8, 2014 by msebar Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 8, 2014 Share Posted October 8, 2014 (edited) Why don't you test them before you start manipulating them? Actually - they will be "set" when you assign those POST vars to them, or else you would get an error/notice that the post vars are not set. So what you really want to test is the POST vars for being set and for being <> blank Edited October 8, 2014 by ginerjm Quote Link to comment Share on other sites More sharing options...
msebar Posted October 8, 2014 Author Share Posted October 8, 2014 The if statement doesn't work the rest of the code does. If I leave the fields blank coming in from the form it echo's the first statemnet. If I fill in the form the first statment still gets echoed and my results don't show. So my issue really is the else is not working Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 8, 2014 Share Posted October 8, 2014 What do you mean by 'doesn't work'? There's nothing wrong with the statements, altho the else echo seems odd with those closing html tags embedded. do you have error checking turned on? Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted October 8, 2014 Solution Share Posted October 8, 2014 An empty form field does have a value, namely the empty string. So the isset() check always succeeds unless you manually remove the form field so that the parameter isn't present at all. If you want to check for empty fields, you need the empty() statement. This covers both missing parameters and empty parameters. 1 Quote Link to comment Share on other sites More sharing options...
msebar Posted October 8, 2014 Author Share Posted October 8, 2014 The empty statement worked. Thanks much Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 8, 2014 Share Posted October 8, 2014 The empty statement worked. Thanks much You may want to consider trim()ing the values before that check. Otherwise, a value of only white-space characters (such as spaces) would cause empty() to return true. 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.