仅使用PHP在同一页面上显示表单验证错误消息?

时间:2017-02-13 13:36:14

标签: php html validation

我是PHP的新手,我在这里拼凑了一些其他答案。谁能告诉我如何显示$ errMsg?目前,空白或不正确的名称会导致空白页面。这是因为表格没有再次显示吗?如果是这样,我该如何使用错误消息“重新加载”表单?

<?php
$name = "Fred";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  if (!empty($_POST["name"])) {

      if ($_POST["name"] == $name) {
        include("welcomeFred.php");
      }

      else {
        $errMsg = "Incorrect name";
      }

  }

  else {
    $errMsg = "Name required";
  }

}
else { ?>

  <html>
  ...
  <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <input type="text" name="name" required>
    <span><?php echo $errMsg;?></span>
    <input type="submit" value="Submit">
  </form>
  ...
  </html>

<?php } ?>

1 个答案:

答案 0 :(得分:1)

您不应该将表单的呈现放在if结构的else中。这就是您提交表单时未加载表单的原因。

删除文件末尾的df <- read.df(csvPath, "csv", header = "true", inferSchema = "true", na.strings = "NA") changeLocation <- function(partitionnedDf) { nrows <- nrow(partitionnedDf) curLocation <- as.integer(1) # Loop over each row of the partitionned data frame for(i in 1:nrows){ row <- partitionnedDf[i,] if(row[1] != "power") { curLocation <- row[2] } partitionnedDf[i,3] <- curLocation } # Return modified data frame partitionnedDf } # Load data df <- read.df("data.csv", "csv", header="false", inferSchema = "true") head(collect(df)) # Define schema of dataframe schema <- structType(structField("action", "string"), structField("value", "integer"), structField("location", "integer")) # Change location of each row df2 <- dapply(df, changeLocation, schema) head(df2) ,它应该可以正常工作。