Reputation: 289
I have the 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 I hit the submit button the words Hello and Good bye are displayed but it is telling me PHP Notice: Undefined index: Now $_POST['address_building_number'
is from an HTML input form with a name address_building_number.
How can I modify if(isset($_POST['address_building_number']) OR !isset($_POST['address_building_number'])){
so that I can 'define the index' while be still able to echo Hello and Good bye.
Upvotes: 0
Views: 1627
Reputation: 74220
<?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 you want the form to NOT appear after submit
// exit;
?>
<form action="" method="post">
Name: <input type="text" name="address_building_number"><br>
<input type="submit" value="Submit">
</form>
Your conditionals seem to be fighting against themselves.
Notice the if(isset
and if(!isset
for the $_POST['address_building_number']
What is happening here is, "if it IS set and if it is NOT set"; being a contradictory statement.
What you probably meant to use was:
if(isset($_POST['address_building_number']) OR !empty($_POST['address_building_number']))
(Quick FYI) If that's the case, ||
has precedence over OR
therefore use:
if(isset($_POST['address_building_number']) || !empty($_POST['address_building_number']))
...which checks to see if it is set, and if it is not empty.
The following has been successfully tested and echo'ed everything with 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) for 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 to "so that I can 'define the index' while be still able to echo Hello and Good bye" - I'm having a hard time understanding what you mean by this.
If what you mean by this is, "if you can still echo "Hello and Good bye" outside of that conditional statement", then the answer is yes; which is just what my answer does, since it's using the OR
(||
) operator. Had it been the AND
(&&
) operator, then that would not be possible.
Upvotes: 3
Reputation: 934
1st - your if statement is wrong it always results in true. I am not sure what you trying to do here?
2nd - PHP Notice: Undefined index is shown because your if statement is always true even if you have submitted form or not.
3rd - maybe change if loop to
if(isset($_POST['submit'])) {
Also make sure submit button has name="submit"
Upvotes: 0