PHP Define index for HTML form $_POST -
i have following php code
<?php if(isset($_post['address_building_number']) or !isset($_post['address_building_number'])){ $address_building_number = $_post['address_building_number']; echo 'hello'; $good = 'good bye'; } echo $address_building_number; echo $good; ?>
when hit submit button words hello , bye displayed telling me php notice: undefined index: $_post['address_building_number'
html input form name address_building_number.
how can modify if(isset($_post['address_building_number']) or !isset($_post['address_building_number'])){
can 'define index' while still able echo hello , bye.
edit
all-in-one version
<?php if(isset($_post['address_building_number']) || !empty($_post['address_building_number'])){ $address_building_number = $_post['address_building_number']; echo 'hello'; $good = 'good bye'; } echo $address_building_number; echo $good; // comment out exit; if want form not appear after submit // exit; ?> <form action="" method="post"> name: <input type="text" name="address_building_number"><br> <input type="submit" value="submit"> </form>
original answer
your conditionals seem fighting against themselves.
notice if(isset
, if(!isset
$_post['address_building_number']
what happening here is, "if is set , if not set"; being contradictory statement.
what meant use was:
if(isset($_post['address_building_number']) or !empty($_post['address_building_number']))
(quick fyi) if that's case, ||
has precedence on or
therefore use:
if(isset($_post['address_building_number']) || !empty($_post['address_building_number']))
...which checks see if set, , if not empty.
the following has been tested , echo'ed no error messages:
html
<form action="action.php" method="post"> name: <input type="text" name="address_building_number"><br> <input type="submit" value="submit"> </form>
php (action.php) example
<?php if(isset($_post['address_building_number']) || !empty($_post['address_building_number'])){ $address_building_number = $_post['address_building_number']; echo 'hello'; $good = 'good bye'; } echo $address_building_number; echo $good; ?>
in regards "so can 'define index' while still able echo hello , bye" - i'm having hard time understanding mean this.
if mean is, "if can still echo "hello , bye" outside of conditional statement", answer yes; answer does, since it's using or
(||
) operator. had been and
(&&
) operator, not possible.
Comments
Post a Comment