I’m having a issue where a variable is becoming undefined when the page is loaded individually… So.
My front page has a address form where when the address is filled out and you click “Get your offer” it’ll take you to another page where the address is carried over using $_POST['address']
in the value of the new input. so value="<?php echo $_POST['address']; ?>"
My problem is that when the offer page is loaded without using the front page form it gives me the error
<br /><b>Notice</b>: Undefined index: address in <b>C:xampphtdocsofferindex.php</b> on line <b>228</b><br />Address
which makes sense. so i tried to fix it by putting this in the value=
:
<?php $carryover = $_POST['address'] or empty($carryover); if(empty($carryover)) { echo 'Enter Address'; } else { echo $carryover; } ?>
which did absolutely nothing so.
Front page form:
<form method="post" action="/offer/index.php" name="front" id="front"> <div class="form-group"> <input type="text" id="autocomplete" onFocus="geolocate()" name="address" id="address" value="" placeholder="123 main st" required> <button type="submit" class="theme-btn btn-style-nine"><span class="txt">Get your offer</span></button> </div> </form>
Form 2 on offer page (where address is carried over too):
<form class="multisteps-form__form" action="finish.php" id="wizard" method="POST" enctype="multipart/form-data"> <div class="col-lg-8"> <div class="form-input-inner position-relative has-float-label" > <input type="text" name="address" id="address" placeholder="Address" value="<?php $carryover = $_POST['address']; if(empty($carryover)) { echo 'Address'; } else{ echo $carryover; } ?> " class="form-control" required> <label>Address</label> <div class="icon-bg text-center"> <i class="fas fa-home"></i> </div> </div> </div> </form
Advertisement
Answer
The undefined index notice happens when you read $_POST['address']
so any code you write after that isn’t going to affect the notice.
Checking whether $carryover is empty is too late, you need to check if the $_POST index itself is empty.
Instead of:
$carryover = $_POST['address']; if(empty($carryover)) { echo 'Enter Address'; } else { echo $carryover; }
You need to use:
if(empty($_POST['address']) { echo 'Enter Address'; } else { $carryover = $_POST['address']; echo $carryover; }