-1

I have a html/php code as shown below in the file abc.php in which at Line#A, I am getting the following error:

Notice: Undefined variable: world in abc.php on line#A  

html/php code (abc.php) :

if ('POST' == $_SERVER['REQUEST_METHOD']) {
    $world = $_POST['world'];
}

<form method="post" action="abc.php?id=<?= $id; ?>">
    <input type="hidden" name="world" value="<?= $world; ?>"/> //line#A
</form>

This is what I have tried but it doesn't seem to work:

if ('POST' == $_SERVER['REQUEST_METHOD']) {
    $world = isset($_POST['world']);
}
flash
  • 1,455
  • 11
  • 61
  • 132
  • 2
    your `$world` is only exists within the scope of the `if` block only. create an empty `$world = ''` before the `if` block should do the trick. though i do curious why i had to comment this given you have 120+ question in php. – Bagus Tesa Jun 19 '22 at 04:17
  • @BagusTesa I have modified my question. My apologies for being not clear before. – flash Jun 19 '22 at 04:29

1 Answers1

3

The $world variable will only be defined if page served via POST method. So, what you need is define it before the condition:

$world = "";
if ('POST' == $_SERVER['REQUEST_METHOD']) {
    $world = $_POST['world'];
}

<form method="post" action="abc.php?id=<?= $id; ?>">
    <input type="hidden" name="world" value="<?= $world; ?>"/> //line#A
</form>
vanowm
  • 9,466
  • 2
  • 21
  • 37