访客日志接受输入,然后显示新消息

时间:2019-04-04 17:59:19

标签: php

当用户访问“访客日志”页面时,他们应该能够看到提示要求他们输入姓名的提示。 提交表单后,同一页面应显示完全不同的消息,欢迎用户访问该网页。 当用户刷新页面时,该过程重新开始。

到目前为止,这是我尝试过的方法,它可以工作,但是我仍然不明白在输入后,谁会显示一条新消息,刷新时并不能清除输入,我必须再次进入网站重启。

    <!DOCTYPE html>

<html lang="en">
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">


        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">


        <link rel="stylesheet" type="text/css" href="store.css">

       <?php include 'navbar.php';?>

    </style>-->
        <title>Visitor Log</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        </head>

        <body class="background-body-color ">


        <div class="centered-container borderColor">

            <h1 id="example-id-name" class=" centered-text sansSerif-text ">Visitor Log</h1>



    <?php
// define variables and set to empty values
$nameErr = "";
$name = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<p2 id="example-id-name" class="centered-text "><center>Please enter your name<center></p>
<p><span class="error"></span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
   <input type="text" name="name" value="<?php echo $name;?>">
  <span class="error"> <?php echo $nameErr;?></span>
  <br><br>
 <input type="submit" name="submit" value="Submit">  
</form>

<?php

echo $name;
echo "<br>";

?>

  </div>
        <script src ="scripts/test.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
        <script src = "scripts/jquery_test.js"></script>


 <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

 </body>
</html>

所有PHP逻辑都应该在store_visitor_log.php上。在此过程中,用户绝不应位于任何其他页面上。 提交表单之前的消息应该与提交表单之后的消息完全不同。这将要求我使用条件语句。 您只需使用PHP即可完成此任务。

this is what it looks like now after entering the name it must change to this

0 个答案:

没有答案