I am trying to run my PHP code, but I am getting an undefined error for the $name variable. The error will show only when it runs for the first time only, but after the second run, it didn't show any error.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $nameerr = "";
if(empty($_POST["name"])){
$nameerr = "This Field is Required";
}
else{
$name = test_run($_POST['name']);
}
}
function test_run($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form action="<?php htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="POST">
<label for="name">Name: </label>
<input type="text" name="name" value="<?php echo $name;?>"><span>* <?php echo $nameerr ?></span><br><br>
<input type="submit" value="Submit">
</form>
<h1>Output</h1>
<?php
echo $name."<br><br>";
?>