누군가이 페이지를 새로 고침 할 때이 양식이 제출되는 이유를 찾도록 도와 줄 수 있습니까? 유효성 검사는 정상적으로 작동하며 세부 사항은 제출시 제출됩니다. 유일한 문제는이 양식이 연락처 페이지를 새로 고침 할 때 제출된다는 것입니다.PHP 코드가있는 양식이 페이지 새로 고침시 제출됩니다
<!doctype html>
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml" class="csstransforms csstransforms3d csstransitions"><head profile="http://gmpg.org/xfn/11">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Our Site</title>
<link href="media-queries.css" rel="stylesheet" type="text/css">
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="fonts.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="pagewrap"> <!--pagewrap start-->
<!--header start-->
<div id="header">
<div class="bottom">
</div><!--bottom end-->
</div> <!--header end-->
<!--content start-->
<div id="content_1">
<div class="content_1_1">
<span class="text9">Contact Us</span><br><hr style="width:670px; margin-left:90px">
<div style="margin-left:90px; margin-right:0px">
<?php
$firstname = $lastname = $email = $telephone = $comments = "";
$error = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
$error = "Field required";
} else {
$firstname = check($_POST["firstname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
$error = "Field required";
}
}
if (empty($_POST["lastname"])) {
$error = "Field required";
} else {
$lastname = check($_POST["lastname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
$error = "Field required";
}
}
if (empty($_POST["email"])) {
$error = "Field required";
} else {
$email = check($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Field required";
}
}
if (empty($_POST["telephone"])) {
$error = "Field required";
} else {
$telephone = check($_POST["telephone"]);
if (!preg_match("/^[0-9\_]{7,20}/",$telephone)) {
$error = "Field required"; }
}
if (empty($_POST["comments"])) {
$errors = "Field required";
} else {
$comments = check($_POST["comments"]);
if (!preg_match("/^[a-zA-Z ]*$/",$comments)) {
$error = "Field required";
}
}
}
function check($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (empty($error)) {
$from = "From: Our Site!"; //Site name
// Change this to your email address you want to form sent to
$to = "[email protected]";
$subject = "Website Form " . $name . "";
$message = "Message from " . $firstname . " " . $lastname . "
Email: " . $email ."
Phone: " . $telephone . "
Comments: " . $comments ."";
mail($to,$subject,$message,$from);
}
?>
<form name="contactform" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<table width="450px">
<tr>
<td valign="top">
<label for="firstname">First Name *</label>
</td>
<td valign="top">
<input type="text" name="firstname" maxlength="50" size="30">
<span class="error" style="color:#C00;"><?php echo $error ;?></span>
</td>
</tr>
<tr>
<td valign="top">
<label for="lastname"> Last Name *</label>
</td>
<td valign="top">
<input type="text" name="lastname" maxlength="50" size="30">
<span class="error" style="color:#C00;"><?php echo $error ;?></span>
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="email" name="email" maxlength="80" size="30">
<span class="error" style="color:#C00;"><?php echo $error ;?></span>
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<input type="number" name="telephone" maxlength="30" size="30">
<span class="error" style="color:#C00;"><?php echo $error ;?></span>
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
<span class="error" style="color:#C00;"><?php echo $error ;?></span>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="submit" name="submit">
</td>
</tr>
</table>
</form>
<br>
</div>
</div> <!--content_1_1 end-->
</div><!--content end-->
<!--footer start-->
<div id="footer">
</div><!--footer end-->
</div><!--pagewrap end-->
</body>
</html>
가장 간단한 방법은 리디렉션을 추가하는 것 성공적으로 다시 리디렉션 같은 페이지로 그런 다음 사용자가 페이지를 새로 고치면 요청에 POST 데이터가 없습니다. – FlabbyRabbit
하지만 처음으로 페이지를 열면 자동으로 우편으로 발송됩니다. – user3355627