Jump to content

display include only if value is posted


webguync

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/222934-display-include-only-if-value-is-posted/
Share on other sites

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');
}
?>

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');
}
?>

 

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');
}
?>

 

 

<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');
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.