如何在php错误语句中使用span标记



当答案不等于8时,echo语句不会打印。因此,如果答案不是8,则应在文本框下方打印红色"请重试"。当我点击注册按钮时,它显示的只是什么都没有。

<?php
session_start();
$nameErr = "";

$answer = $_POST['lastname'];
if ($_POST['reg']){

if ($answer != '8') {
echo "<span style='color:red'>Please enter Correct Answer</span>";
}
}
?>
<!Doctype html>
<head>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" 
type="text/css">
<link href="bootstrap/css/styles.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="Register.css">
<script src='https://www.google.com/recaptcha/api.js'></script>
<title> Register</title>

</head>
<html>
<body>
<form method="post" action="">
What is 5 + 3 =<br>
<input type="text" name="lastname"> 


<div class="row">
<div class="form-group col-xs-5 col-xs-offset-3.5">
<input class="btn btn-primary btn-lg btn-block"  value="Register" 
type="submit" name="reg">

您需要将echo放在文档的<body>中。此外,<html>需要位于顶部。

<?php
session_start();
$nameErr = "";
?>
<!Doctype html>
<html>
<head>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" 
type="text/css">
<link href="bootstrap/css/styles.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="Register.css">
<script src='https://www.google.com/recaptcha/api.js'></script>
<title> Register</title>
</head>
<body>
<form method="post" action="">
What is 5 + 3 =<br>
<input type="text" name="lastname"> 
<?php
if (isset($_POST['reg'])){
$answer = $_POST['lastname'];
if ($answer != '8') {
echo "<span style='color:red'>Please enter Correct Answer</span>";
}
}
?>
<div class="row">
<div class="form-group col-xs-5 col-xs-offset-3.5">
<input class="btn btn-primary btn-lg btn-block"  value="Register" type="submit" name="reg">
</div>
</div>
</form>
</body>
</html>

在实际HTML代码上方打印错误span。只需将if语句下移即可。比如:

...
<div class="row">
<?php
if ($answer != '8') {
echo "<span style='color:red'>Please enter Correct Answer</span>";
}
?>
</div>
<div class="form-group col-xs-5 col-xs-offset-3.5">
...

相关内容

  • 没有找到相关文章

最新更新