如果用户输入空格,我想避免验证表格 但实际上进入空格表格仍然是有效的......
<?php
$name = ""; $email = ""; $comment = ""; $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if(!empty($_POST["name"]) && !empty($_POST["email"]) && !empty($_POST["website"]) && !empty($_POST["comment"])){
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
echo htmlspecialchars("".$name."".$email."".$website."".$comment."");
}else{
echo htmlspecialchars("fill all fields");
}
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
我做错了什么?
答案 0 :(得分:1)
我认为您应该在修剪后检查数据是否为空您可以尝试以下代码
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
if(!empty($name) && !empty($email) && !empty($website) && !empty($comment)){
echo htmlspecialchars("".$name."".$email."".$website."".$comment."");
}else{
echo htmlspecialchars("fill all fields");
}
}
答案 1 :(得分:0)
只是空格尝试
$str = ' ';
if (ctype_space($str)) {}
或
if(trim($str) == '') {} // check a blank string or it's a whitespace
根据您的要求安排