Php,如何显示简单空白表单提交的错误?
表单htmlPhp,如何显示简单空白表单提交的错误?,php,forms,Php,Forms,表单html 有什么方法可以让这个网站更安全吗 <form action="" method="post"> <input name="example" type="text"> <input name="submit" type="submit"> </form> Php代码 如何显示错误消息为空 <?php
有什么方法可以让这个网站更安全吗
<form action="" method="post">
<input name="example" type="text">
<input name="submit" type="submit">
</form>
Php代码
如何显示错误消息为空
<?php
if (isset($_POST['submit'])) {
$example = $_POST['example'];
function myfunction() {
$names = array(
"sumon",
"bamon",
"timon",
);
return $names[rand ( 0 , count($names) -1)];
}
for ($x = 0; $x <= 10; $x++) {
echo $example.myfunction();
}
}
?>
只需检查
示例
输入是否像提交
if ( isset($_POST['example']) && $_POST['example'] == "" ) {
// example input is empty
die("example input is empty");
}
isset($\u POST['example'])
将检查example
在$\u POST
数组中是否存在键入
然后,$\u POST['example']==“将检查示例
输入是否为空请使用下面的示例验证。
Please use the below sample validation .
<?php
// define variables and set to empty values
$nameErr = $emailErr = "";
$name = $email = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{
$nameErr = "Name is required";
}
else
{
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"]))
{
$emailErr = "Email is required";
}
else
{
$email = test_input($_POST["email"]);
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<input type="submit" name="submit" value="Submit">
</form>
为什么simple不在表单html中使用required
“有什么方法可以使表单更安全?”-在什么意义上?什么是不安全的?“更安全”。。。但你问的是如何阻止人们发送空白数据。为了避免将来的混淆,您在这里要求的是验证。验证与安全性不同。请执行一些前端验证:isset
不检查是否为空。这个POST变量将被设置为“
。