webguync Posted December 29, 2010 Share Posted December 29, 2010 Hi, I only want to display some included PHP code only if a value is displayed from form input. The value is a zip code which is used to display weather information, but the app produces an error when you first enter the page, because their is no value for the zip variable. I know I need an if/else statement, but not sure what to put in it. The form code is this <h3>Enter your zip code to receive weather info.</h3> <form method="post" action="<?php echo $PHP_SELF;?>"> Enter Zip Code:<input type="text" size="12" maxlength="12" name="zip"><br /> <input id="submit" type="submit" name="Submit" value="Enter Zip"/> </form> <?php // this is how we bring in the weather file include 'weather_report.php' ?> the zip variable is set-up in the weather_report.php file as follows $zip = $_POST["zip"]; but like I said until that value is set, an error is thrown by the weather app code. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 29, 2010 Share Posted December 29, 2010 something like if the value is set, include? you are practically writing the code with your question. why not give it a try? Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 29, 2010 Share Posted December 29, 2010 in a very simplistic psuedo code way (no error checking, cleansing, etc) <?php $zip = $_POST['zip']; /* check to see if its set, if its numeric, if it has proper length etc etc */ /* if the above check produce and error message ie $error_mess = some content */ /* then send back to form */ if(errors) { back to form }else{ include ('weather_report.php'); } ?> Quote Link to comment Share on other sites More sharing options...
webguync Posted December 29, 2010 Author Share Posted December 29, 2010 thanks, is this the way to do the variable check? I ask b/c it doesn't seem to be working. I don't get the included code. <form method="post" action="<?php echo $PHP_SELF;?>"> Enter Zip Code:<input type="text" size="12" maxlength="12" name="zip"><br /> <input id="submit" type="submit" name="Submit" value="Enter Zip"/> </form> <?php $zip = $_POST['zip']; /* check to see if its set, if its numeric, if it has proper length etc etc */ /* if the above check produce and error message ie $error_mess = some content */ /* then send back to form */ if (!isset($zip)) { echo "please fill in a zip code value"; } else{ include ('weather_report.php'); } ?> Quote Link to comment Share on other sites More sharing options...
webguync Posted December 30, 2010 Author Share Posted December 30, 2010 does anyone see anything wrong with the code I posted? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 30, 2010 Share Posted December 30, 2010 unless you've set it somewhere this value is empty: $PHP_SELF; Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 30, 2010 Share Posted December 30, 2010 other than that undefined value and a notice about $_POST['zip'] being an undefined index, it works for me. i submit a zip code and the include is included. You might want to turn your errors up all the way to get warnings and notices. Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted December 30, 2010 Share Posted December 30, 2010 don't forget to filter $PHP_SELF http://www.phpro.org/tutorials/PHP-Security.html Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 30, 2010 Share Posted December 30, 2010 also don't forget it's $_SERVER['PHP_SELF']. you can leave it out entirely, using action='' (empty). Quote Link to comment Share on other sites More sharing options...
webguync Posted December 30, 2010 Author Share Posted December 30, 2010 so after reading these responses I believe this is what the form and if/else code should look like? <form method="post" <?php echo filter_var($_SERVER['PHP_SELF'], FILTER_SANITIZE_STRING);?> Enter Zip Code:<input type="text" size="12" maxlength="12" name="zip"><br /> <input id="submit" type="submit" name="Submit" value="Enter Zip"/> </form> <?php $zip = $_POST['zip']; echo $zip; /* check to see if its set, if its numeric, if it has proper length etc etc */ /* if the above check produce and error message ie $error_mess = some content */ /* then send back to form */ if (!isset($_POST['zip'])) { echo "please fill in a zip code value"; } else{ include ('weather.php'); } ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 30, 2010 Share Posted December 30, 2010 <form method="post" action=''> Enter Zip Code:<input type="text" size="12" maxlength="12" name="zip"><br /> <input id="submit" type="submit" name="Submit" value="Enter Zip"/> </form> <?php if (!isset($_POST['zip'])) { echo "please fill in a zip code value"; } else{ include ('weather.php'); } ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 30, 2010 Share Posted December 30, 2010 but you'll probably want to make sure the form was POST'ed before checking to see if $_POST['zip'] isset. otherwise, you'll get the error before you submit the form. Quote Link to comment Share on other sites More sharing options...
webguync Posted December 30, 2010 Author Share Posted December 30, 2010 not sure I follow, how would I check that? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 30, 2010 Share Posted December 30, 2010 <?php if ($_SERVER['REQUEST_METHOD'] == "POST") { if (!isset($_POST['zip'])) { echo "please fill in a zip code value"; } else { include ('weather.php'); } } ?> Quote Link to comment Share on other sites More sharing options...
webguync Posted December 31, 2010 Author Share Posted December 31, 2010 seems to be working, thanks! 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.