SkyRanger Posted July 18, 2015 Share Posted July 18, 2015 I am having a problem when a user just hits submit on an empty field and having all entries displayed. Is there a way to stop this with the code I have and go right to the else { if ( isset( $_POST['hp-submitted'] ) ) { // sanitize form values $houseaddy = sanitize_text_field( $_POST["haddy"] ); global $wpdb; #$results = $GLOBALS['wpdb']->get_results("SELECT * FROM locations where stname='$houseaddy'"); $rowhs = $wpdb->get_results( "SELECT * FROM locations where stname like '%" . $houseaddy ."%'"); if (!empty($rowhs)) { foreach ( $rowhs as $rowhs ) { echo "<br />"; echo "<p><b><font color=\"#92AEFE\" size=\"4\">Report Address: </font><font size=\"4\">"; echo $rowhs->stname . ", " . $rowhs->hcity ." ". $rowhs->hprov . "</font></b><br>"; echo "<b><font size=\"4\" color=\"#92AEFE\">Report Number: </font><font size=\"4\">" . $rowhs->hid ."</font></b><br>"; echo "<b><font size=\"4\" color=\"#92AEFE\">Date of Report: </font><font size=\"4\">" . $rowhs->datepost ."</font></b></p>"; } } else { echo "0 entries available"; echo "<br /><br />"; } } Quote Link to comment Share on other sites More sharing options...
inversesoft123 Posted July 18, 2015 Share Posted July 18, 2015 (edited) Hello trim it and check it is blank or no. if(trim($_POST["haddy") != "") { Edited July 18, 2015 by inversesoft123 Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted July 18, 2015 Author Share Posted July 18, 2015 (edited) Perfect thank you, that fixed one problem but caused another. Now when box is empty goes to else { echo "0 entries available"; but when somebody enters an entry that does not exist just shows blank. Is there a way to combine if (!empty($rowhs)) { with if(trim($_POST["haddy"]) != "") { to show else { echo "0 entries available"; Edited July 18, 2015 by SkyRanger Quote Link to comment Share on other sites More sharing options...
Solution QuickOldCar Posted July 18, 2015 Solution Share Posted July 18, 2015 if (!empty($rowhs) && trim($_POST["haddy"]) != "") { Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted July 18, 2015 Author Share Posted July 18, 2015 Awesome, just what I was looking for thank you very much 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.