-1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register Page</title>
<link rel="stylesheet" href="css/form.css">

</head>
<body>
<?php
$submit = $_POST["submit"];
$fullName = $_POST["fullname"];
if(isset($submit)) {
  if (empty($fullname)) {
  array_push($errors, "All fields are required!");
}
if (count($errors)>0) {
foreach($errors as $error) {
  echo $error;
  }
}
else{
//pass
}
}
?>

<form class="form" action="registeration.php" method="post">
<h1 class="heading">Register</h1>     
<input type="text" placeholder="name" name="fullname" autocomplete="off" class="name"/>
<input type="submit" name="submit" value="register"/>
</form>
</body>
</html>

if i run it then It shows "All fields are required!" before i click the button
i want to show it as error it should show only if someone click the button who forget/didn't enter the name gets the error "All fields are required!"

  • 1
    You created `$submit`, so of course, it's set. Maybe you're looking for [!empty](https://www.php.net/empty)? With your code, though, you'll be getting an 'array key doesn't exist' if it hasn't been submitted. – aynber Apr 05 '23 at 14:19
  • You put all the code in the single file. Code is evaluated left to right, top to bottom. It will always evaluate before the form. You need to separate your HTML and PHP code info different files and make a request to the backend – Mike Szyndel Apr 05 '23 at 14:19
  • 2
    Using `isset($_POST["submit"])` directly is probably a better approach. The `$submit` variable in this case is *always* set, regardless of what *value* is being set to it. But the *array key* might not be set. – David Apr 05 '23 at 14:20
  • i'll try but i've already tried with/without variable driect, i am to try Mike Syzndel's opinion – pinocchi's dev Apr 05 '23 at 15:07
  • Duplicate of [Checking if form has been submitted - PHP](https://stackoverflow.com/questions/7711466/checking-if-form-has-been-submitted-php) . Keeping the code in separate files is another possible solution, but it's certainly not essential, and sometimes that has disadvantages too. David's approach will also work (if you think you already tried it, then I would assume you didn't implement it correctly). – ADyson Apr 05 '23 at 15:18
  • @ADyson bro i tried it works for the first time but , now its permanent it should only show if button is pressed and after a reload it should be default again – pinocchi's dev Apr 05 '23 at 16:21
  • `it should only show if button is pressed`...which is exactly what will happen. It can't be permanent unless, again, you implemented it wrongly. – ADyson Apr 05 '23 at 17:54

1 Answers1

-3

You put all the code in the single file. Code is evaluated left to right, top to bottom. It will always evaluate before the form. You need to separate your HTML and PHP code info different files and make a request to the backend – by @Mike Szyndel

  • 1
    To improve your answer, consider addressing the question of why isset is not working. Since the $submit variable was set before the isset check, it means that the variable is already set. Therefore, you can directly use isset($_POST["submit"]) or check if it's empty. – satya prakash patel Apr 11 '23 at 07:21