Variable not changing when form is filled out

I have a page where I want to pull up information based on zipcodes. When a person goes to the page on the first visit the zipcode $variable is null because they haven’t filled out the form and the page gives me an “undefined variable” error. So, I wrote this little if/else statement to fix it but it isn’t working like I had hoped.

(form goes here to input zipcode using method = “POST”)

<?php if ($zipcode = 'null' ) { ( $zipcode = '83777' ); } else { ($zipcode = $_POST['zipcode'] ); } The variable appears in my 'echo' statement as 83777 on the first visit but when the form is filled out the new zipcode isn't passed along. For the record everything works without the if/else statement except for the initial "undefined variable" error.

Try this:

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

if( isset( $_POST['zipcode'] ) ):
	echo '<br>Already set: ' .$_POST['zipcode'];
else:
	$_POST['zipcode'] = '83777';
	echo '<br>Just set: ' .$_POST['zipcode'];
endif;
$zipcode = $_POST['zipcode'];

echo '<br>final: ' .$zipcode;
echo '<pre>'; print_r( $_POST ); echo '</pre>';

If you are using PHP7 I prefer setting the default value like this:

// PHP7 specific
   echo '<br>Default: ',
   $zipcode = $_POST['zipcode'] ?? '83777';

1 Like

That worked great… Thank you. I had to strip it down but it is doing exactly what I wanted it to.

It didn’t like the ‘declare’ statement.
Here is the final product.

error_reporting(-1);
ini_set(‘display_errors’, ‘1’);
if( isset( $_POST[‘zipcode’] ) ):
else:
$_POST[‘zipcode’] = ‘83706’;
endif;
$zipcode = $_POST[‘zipcode’];

Looks like I tried to define the $variable instead of the array itself. Lesson learned. Thanks again.

1 Like

Declare is a new PHP7 feature.

Try this to view your php version:

<?php 

phpinfo();

That in itself is fine, it’s just that you weren’t using isset() to check if the variable exists, you were comparing it to ‘null’, and you were comparing the variable you haven’t created yet instead of the $_POST variable. There’s a difference between a variable not existing and one that is equal to ‘null’. Another way would have been to just change your first line to

if (!isset($_POST['zipcode'])) { 

for the same effect. So if the $_POST variable does not exist, set $zipcode to a fixed value, otherwise set it to the value of the $_POST variable.

However the error-reporting lines in the post from @John_Betong are useful to set during the development stage.

He was assigning it instead of comparing …

So he was.

Thanks for the help on this. I’m new to PHP and am STRUGGLING with it. I feel like I learn 5 things one day and forget 4 of them by the next day.

phpinfo(); = PHP Version 5.6.23

I have XAMPP setup on my computer.

1 Like

Practice frequently and learn to love the Online Manual which is one of the best manuals I have encountered and also has numerous examples.

PHP7 has some very nice features and is provided by most hosts.

I forgot to mention that the following should be set differently if the script is used online to prevent errors and warnings being displayed on the screen.

Try this:

<?php 
define('LOCALHOST', 'localhost' === $_SERVER[['SERVER_NAME'); 

// SET ONLINE DEFAULTS
   error_reporting(-1);
   ini_set('display_errors', '0');  // should save errors and warnings to a log file

if( LOCALHOST ):
  ini_set('display_errors', '1');
endf;

Thanks for the information. It worked well. I did have a few more problems with the form but unrelated to your fix. I have been able to figure them out with a lot of searching and trial and error.

I’ve started using the online manual as you recommend. Another great resource is the w3schools website. Short and sweet.

Thanks again for all the help.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.