将用户重定向到窗体并弹出错误消息



我是PHP的新手。如何将用户重定向到表单并在用户无法填写所有文本字段时弹出错误消息?

这是我的代码:

<div class = 'register'>
<form action = "registration.php" method="post">
<p>Name:<br> <input type="text" name="name"></p>
<p>Student No:<br>  <input type="number" name="studentNo"></p>
<input type="submit" onclick="load()" name="submit" value="Submit">
</form>
</div>
<br>
<?php   
if(isset($_POST['name']) && (isset($_POST['studentNo']))){
$name = $_POST['name'];
$studentNo = $_POST['studentNo'];
echo "Name: ".$name."<br>";
echo "Student No:".$studentNo."<br>";
}
else{
}
?>

使用2020年的最新技术怎么样?

表格

您可以直接在客户端验证 html 表单,以最大程度地减少发生的错误。为此,只需使用 html5 约束验证 api。

<form id="my-form" action="registration.php" method="post">
<label>
Username
<input type="text" name="username" value="" required>
</label>
<label>
Number
<input type="number" name="number" value="" required min="1" max="9999" step="1">
</label>
<input type="submit" name="submit" value="send">
</form>

如您所见,输入元素具有不同的类型属性。例如,我没有取你的名字和数字元素。第一个输入元素是用户名,因此我们使用文本输入元素。此元素可以接受每个输入。 第二个输入元素是数字类型。在这里,您只能输入数值。 这两个元素都具有必需的属性。如果不填写任何内容,您将无法提交表格。您必须输入用户名和号码。

使用 PHP 进行服务器端验证

由于我们有客户端验证,因此您必须再次验证服务器上接收到的数据。永远不要相信任何用户输入!在服务器上检索数据只是意味着用户已经填写了一些东西。它不是书记员,如果输入是那个,你所期望的。

if (isset($_POST['submit'])) {
$valid = true;
$errorType = '';

if (!isset($_POST['username']) || !strlen(trim($_POST['username'])) { 
$valid = false;
$errorType = 'username';
}
if (!isset($_POST['number']) || !intval($_POST['number'])) {
$valid = false;
$errorType = 'number';
}
if (!$valid) {
header('registration.php?error=' . $errorType);
exit();
}
}

上面显示的代码检查是否点击了提交按钮。如果是这样,则检查两个输入元素,如果它们不是 empi。您可以在此处添加更多验证。对于用户名,最小字符串长度是可行的。如果数据库中存在该编号,则可以根据数据库内容检查该编号。

在 html 中显示错误

由于我们已经使用 php 标头函数在重定向中定义了 GET 参数error,因此我们可以在标记中使用它。

<form id="my-form" action="registration.php" method="post">
<label>
Username
<input type="text" name="username" value="" required>
<?php if (isset($_GET['error']) && $_GET['error'] === 'username') :?>
<p>Yo! We need your username, dude!</p>
<?php endif; ?>
</label>
<label>
Number
<input type="number" name="number" value="" required min="1" max="9999" step="1">
<?php if (isset($_GET['error']) && $_GET['error'] === 'number') :?>
<p>Yeah ... entering a number is not that hard.</p>
<?php endif; ?>
</label>
<input type="submit" name="submit" value="send">
</form>

选择

有的。当您在表单元素中使用必需属性时,您可以使用 JavaScript 来捕获 HTML5 约束验证 API 引发的错误。API 显示发生的错误,您可以显示有关发生的错误的更具体的错误消息。

另一种选择可以是 AJAX。如果约束验证 API 最终得到有效结果,请使用同步 JavaScript 请求发送输入数据。这样,页面就不会在服务器端发生任何错误时重新加载。用户留在网站上,成功后将被重定向。

但在使用此高级技术之前,请先了解如何在 php 中使用简单的 html 表单;)。

if (isset($_POST['submit'])) {
if (empty($_POST['name'])) {
echo '<script language="javascript">';
echo 'alert("Name is required")';
echo '</script>';
echo '<script language="javascript">';
echo 'window.location.href = "yourpage.php"';
echo '</script>';
}
if (empty($_POST['studentNo'])) {
echo '<script language="javascript">';
echo 'alert("Student name is required")';
echo '</script>';
echo '<script language="javascript">';
echo 'window.location.href = "yourpage.php"';
echo '</script>';
}
}

相关内容

最新更新