Форма с 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 = "info@oursite.com";
$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>

0

Решение

Оберните весь ваш PHP внутри следующего условного оператора, так как у вас уже есть именованная кнопка отправки.

<?php
if(isset($_POST['submit'])){
$firstname = $lastname = $email = $telephone = $comments = "";

...

mail($to,$subject,$message,$from);
}

} // brace for if(isset($_POST['submit']))

?>

Однако вы можете разделить HTML-форму и PHP на два отдельных файла, используя тот же условный оператор, и перенаправить на другую страницу впоследствии.

Чтобы узнать, как перенаправить, смотрите следующее Q&А на стеке:

1

Другие решения

Ты используешь:

if (empty($error)) {

mail();

}

На освежающий $error вероятно, пусто, и поэтому форма отправляется по почте.

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector