我是PHP的新手,我想知道当我点击提交按钮时,如何让我的所有文本框都不为空。当我输入像space这样的特殊字符时,/;><等等所有文本框都变空。我只希望我输入特殊字符的文本框为空而不是所有文本框。
这是我的代码
function.php
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="1.php">
<label>Name:</label> <input type="text" name="name"> <span class="error">* <?php echo $_SESSION['name'];?></span>
<br><br>
<label>E-mail:</label> <input type="text" name="email"> <span class="error">* <?php echo $_SESSION['email'];?></span>
<br><br>
<label>Website:</label> <input type="text" name="website"> <span class="error"><?php echo $_SESSION['website'];?></span>
<br><br>
<label>Comment:</label> <input type="text" name="comment">
<br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $_SESSION['gender'];?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
unset($_SESSION['name']);
unset($_SESSION['email']);
unset($_SESSION['website']);
unset($_SESSION['comment']);
unset($_SESSION['gender']);
?>
1.PHP
<?php
session_start();
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
{$_SESSION['name']= "Name is requiredassssssssssssssss";}
}
$email = test_input($_POST["email"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$email)) {
{$_SESSION['email']= "email is requiredassssssssssssssss";}
}
$website = test_input($_POST["website"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$website)) {
{$_SESSION['website']= "website is requiredassssssssssssssss";}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ((!preg_match("/^[a-zA-Z ]*$/",$name)) || (!preg_match("/^[a-zA-Z ]*$/",$email)) || (!preg_match("/^[a-zA-Z ]*$/",$website)))
{
header("Location: function.php");
}
echo $_POST['name'];
echo $_POST['email'];
echo $_POST['website'];
?>
答案 0 :(得分:2)
试试这样..
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="1.php">
<label>Name:</label> <input type="text" name="name" value="<?php echo $_SESSION['name_value'];?>" > <span class="error">* <?php echo $_SESSION['name'];?></span>
<br><br>
<label>E-mail:</label> <input type="text" name="email" value="<?php echo $_SESSION['email_value'];?>"> <span class="error">* <?php echo $_SESSION['email'];?></span>
<br><br>
<label>Website:</label> <input type="text" name="website" value="<?php echo $_SESSION['website_value'];?>"> <span class="error"><?php echo $_SESSION['website'];?></span>
<br><br>
<label>Comment:</label> <input type="text" name="comment">
<br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $_SESSION['gender'];?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
unset($_SESSION['name']);
unset($_SESSION['email']);
unset($_SESSION['website']);
unset($_SESSION['comment']);
unset($_SESSION['gender']);
unset($_SESSION['name_value']);
unset($_SESSION['email_value']);
unset($_SESSION['website_value']);
?>
<?php
session_start();
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$_SESSION['name_value'] = $name;
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
{
$_SESSION['name']= "Name is requiredassssssssssssssss";
$_SESSION['name_value']= "";
}
}
$email = test_input($_POST["email"]);
$_SESSION['email_value'] = $email;
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$email)) {
{
$_SESSION['email']= "email is requiredassssssssssssssss";
$_SESSION['email_value']= "";
}
}
$website = test_input($_POST["website"]);
$_SESSION['website_value'] = $website;
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$website)) {
{
$_SESSION['website']= "website is requiredassssssssssssssss";
$_SESSION['website_value']= "";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ((!preg_match("/^[a-zA-Z ]*$/",$name)) || (!preg_match("/^[a-zA-Z ]*$/",$email)) || (!preg_match("/^[a-zA-Z ]*$/",$website)))
{
header("Location: function.php");
}
echo $_POST['name'];
echo $_POST['email'];
echo $_POST['website'];
?>
希望这会有所帮助。