如何让php上的代码在我点击提交按钮后立即执行?

如何让php上的代码在我点击提交按钮后立即执行?,php,html,Php,Html,我试图创建一个错误消息,以防用户没有正确填写字段,但在第一次加载页面时,我希望它不显示错误消息,但它在按下按钮之前执行代码,因此在用户采取任何操作之前显示错误消息 我该如何着手解决它 <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>ROCK PAPER SCISSORS GAME</title>

我试图创建一个错误消息,以防用户没有正确填写字段,但在第一次加载页面时,我希望它不显示错误消息,但它在按下按钮之前执行代码,因此在用户采取任何操作之前显示错误消息

我该如何着手解决它

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>ROCK PAPER SCISSORS GAME</title>
  </head>
  <style media="screen">
    input[type=text], select{
      width: 100%;
      padding: 5px;
      margin: 10px;
      height: 20px;
      border-radius: 5px;

    }
    input[type=password]{
      width: 100%;
      padding: 5px;
      margin: 10px;
      height: 20px;
      border-radius: 5px;
    }
    input[type=submit]{
      margin-left: 10px;
      width: 150px;
      height: 30px;
      background-color: #6DC066;
      border-radius: 15px;
    }
    input[type=submit]:hover {
      background-color: #45a049;
    }

    input:hover {
      border-color: #26c8ff;
      border-width: 3px;

    }

  </style>
  <?php

    $responseMessage = "";

    if (!empty($_POST["email"]) && !empty($_POST["pass"])){
      $responseMessage = "they are set";

    } else if (empty($_POST["email"]) && empty($_POST["pass"])){
      $responseMessage = "please, fill in all fields";

    }

   ?>

  <body>
    <div style="width: 550px;">
    <h1 style="margin-left: 10px;">LOGIN ROCK SCISSORS GAME:</h1>
    <p style="margin-left: 10px; color: red"><?= $responseMessage; ?></p>

    <form class="" action="index.php" method="post">
      <label style="margin: 10px;" for="email" >Email:</label>
      <input type="text" name="email" value="" placeholder="Type your email">
      <label style="margin: 10px;" for="pass">Password:</label>
      <input type="password" name="pass" value="" placeholder="Type your password">
      <input type="submit" name="login" value="login">
  </form>
</div>

  </body>
</html>

石头剪刀布游戏
输入[类型=文本],选择{
宽度:100%;
填充物:5px;
利润率:10px;
高度:20px;
边界半径:5px;
}
输入[类型=密码]{
宽度:100%;
填充物:5px;
利润率:10px;
高度:20px;
边界半径:5px;
}
输入[类型=提交]{
左边距:10px;
宽度:150px;
高度:30px;
背景色:#6DC066;
边界半径:15px;
}
输入[类型=提交]:悬停{
背景色:#45a049;
}
输入:悬停{
边框颜色:#26c8ff;
边框宽度:3倍;
}
登录石头剪刀游戏:

电邮: 密码:
您需要检查post请求

<?php
    $responseMessage = "";
    if ($_POST && $_POST["login"]){
        if (!empty($_POST["email"]) && !empty($_POST["pass"])){
            $responseMessage = "they are set";
        }else if (empty($_POST["email"]) || empty($_POST["pass"])){
            // i changed && to || (or)
            $responseMessage = "please, fill in all fields";
        }
    }
?>

在运行代码之前,检查表单是否已提交(通过检查是否存在提交按钮)


<?php

$responseMessage = "";

// This check will only evaluate as true if $_POST['login'] exits and isn't null, 
// which will only be the case when the form been submitted.
if (isset($_POST['login'])) {

    if (!empty($_POST["email"]) && !empty($_POST["pass"])){
        $responseMessage = "they are set";

    } else if (empty($_POST["email"]) && empty($_POST["pass"])){
        $responseMessage = "please, fill in all fields";

    }
}
?>