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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.