我有一个包含3个字段的表单,我想验证这些字段。
让我们说用户犯了错误或没有引入电子邮件,然后所有其他字段(姓名和地址)重置为空白。
有没有办法在不重置名称和地址的情况下显示错误消息(需要电子邮件),以便用户不必再次引入所有值?
对于表单具有多个值的用户来说,这是一个很好的帮助。
非常感谢
请参阅下面的PHP代码:
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $addressErr = "";
$name = $email = $address = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["address"])) {
$AddressErr = "Address is required";
} else {
$address = test_input($_POST["address"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$address)) {
$addressErr = "Only letters and white space allowed";
}
}
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" name="name" placeholder="First Name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
<input type="text" name="email" placeholder="Email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
<input type="text" name="address" placeholder="Address">
<span class="error">* <?php echo $AddressErr;?></span>
<br><br>
<input type="submit" name="submit" value="SAVE">
</form>
<?php
if ($nameErr == '' && $emailErr == '' && $AddressErr == '')
{
$db = pg_connect('host=localhost dbname=test user=samuelraul password=naikaerikamber');
$firstname = pg_escape_string($_POST['name']);
$emailaddress = pg_escape_string($_POST['email']);
$address = pg_escape_string($_POST['address']);
$query = "INSERT INTO host(firstname, emailaddress, address) VALUES('" . $firstname . "', ' '" . $emailaddress . "', '" . $address . "')";
$result = pg_query($db, $query);
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
exit();
}
pg_close();
}
?>
</body>
</html>
答案 0 :(得分:2)
您可以将value="<?php echo $name;?>"
用于名称字段,因为您已定义$name
变量。您可以分别使用$email
和$address
变量用于电子邮件和地址字段
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" name="name" placeholder="First Name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
<input type="text" name="email" placeholder="Email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
<input type="text" name="address" placeholder="Address" value="<?php echo $address;?>">
<span class="error">* <?php echo $AddressErr;?></span>
<br><br>
<input type="submit" name="submit" value="SAVE">
</form>
答案 1 :(得分:1)
通过PHP的最简单方法如下:
<input type="text" name="name" placeholder="First Name" value="<?php echo ( isset( $_POST['name'] ) ? htmlspecialchars($_POST['name']) : '' ); ?>">
此代码检查POST [name]是否已发送到服务器,如果是,则只将内容设置为输入值。
对于其他输入,您必须将$ _POST [name]替换为例如$ _POST [电子邮件]。
另外,我会将输入类型设置为email
,用于电子邮件地址。