[英]Contact Form without ajax just php and html page
我有一個簡單的聯系表,它工作得非常好,但我希望結果在同一頁面上的一個小框中,就在失敗消息即將到來和成功的表單下方
<form method="post" action="contact.php" name="contactform" id="contactform">
<div class="one_half">
<input name="name" type="text" id="name" size="30" onfocus="if(this.value == 'Name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Name'; }" value="Name">
<input name="alter" type="text" id="alter" size="3" onfocus="if(this.value == 'Alter') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Alter'; }" value="Alter">
<input name="email" type="text" id="email" size="30" onfocus="if(this.value == 'E-Mail') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'E-Mail'; }" value="E-Mail">
<input name="phone" type="text" id="phone" size="30" onfocus="if(this.value == 'Handynummer') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Handynummer'; }" value="Handynummer">
<input name="facebook" type="text" id="facebook" size="200" onfocus="if(this.value == 'Facebook') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Facebook'; }" value="Facebook">
<input name="instagram" type="text" id="instagram" size="200" onfocus="if(this.value == 'Instagram') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Instagram'; }" value="Instagram">
</div>
<div class="one_half last">
<textarea name="comments" cols="40" rows="3" id="comments" onfocus="if(this.value == 'Nachricht') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Nachricht'; }">Nachricht</textarea>
</div>
<input type="submit" class="send_message" id="submit" value="Senden"/>
</form>
這里是 PHP 文件
<?php
if (!$_POST) exit;
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$alter = $_POST['alter'];
$facebook = $_POST['facebook'];
$instagram = $_POST['instagram'];
if (get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$address = "xxx@xxx.de";
$e_subject = 'Contact from ' . $name;
$e_body = "von: $name" . PHP_EOL . PHP_EOL;
$e_reply = "Alter: $alter\r\nE-mail: $email\r\nHandynummer: $phone";
$e_content = "Nachricht:\r\n$comments" . PHP_EOL . PHP_EOL;
$e_links = "Facebook:\r\n$facebook\r\nInstagram:\r\n$instagram" . PHP_EOL . PHP_EOL;
$msg = wordwrap($e_body . $e_links . $e_content . $e_reply, 70);
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if (mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Bewerbung erfolgreich</h1>";
echo "<p>Danke <strong>$name</strong>, deine Bewerbung wurde erfolgreich an uns gesendet</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'FEHLER!';
}
它工作完美
但它總是進入一個新站點,無論是錯誤還是成功。
我想在顯示所有結果的表單下添加一個小塊
我希望你能幫助我。
我不想要 ajax 或其他東西。 我只是希望它按原樣添加。
您的問題的解決方案可能是在同一個 PHP 文件中包含表單渲染的邏輯。 這樣做,表單操作將是同一個 PHP 文件,因此它將在呈現表單視圖之前加載 PHP 代碼。 這樣,您就可以根據發送電子郵件的輸出呈現您想要的任何形式的表單。
例如,仔細看看$mailResult
變量:
<?php
$name = $_POST['name'] ?? null;
$email = $_POST['email'] ?? null;
$phone = $_POST['phone'] ?? null;
$comments = $_POST['comments'] ?? null;
$alter = $_POST['alter'] ?? null;
$facebook = $_POST['facebook'] ?? null;
$instagram = $_POST['instagram'] ?? null;
if (get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$address = "xxx@xxx.de";
$e_subject = 'Contact from ' . $name;
$e_body = "von: $name" . PHP_EOL . PHP_EOL;
$e_reply = "Alter: $alter\r\nE-mail: $email\r\nHandynummer: $phone";
$e_content = "Nachricht:\r\n$comments" . PHP_EOL . PHP_EOL;
$e_links = "Facebook:\r\n$facebook\r\nInstagram:\r\n$instagram" . PHP_EOL . PHP_EOL;
$msg = wordwrap($e_body . $e_links . $e_content . $e_reply, 70);
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
$mailResult = '';
if (isset($name, $email)) {
if (mail($address, $e_subject, $msg, $headers)) {
$mailResult = "<fieldset>";
$mailResult .= "<div id='success_page'>";
$mailResult .= "<h1>Bewerbung erfolgreich</h1>";
$mailResult .= "<p>Danke <strong>$name</strong>, deine Bewerbung wurde erfolgreich an uns gesendet</p>";
$mailResult .= "</div>";
$mailResult .= "</fieldset>";
} else {
$mailResult .= 'FEHLER!';
}
}
?>
<html lang="en">
<head>
<title>Title page!</title>
</head>
<body>
<form method="post" name="contactform" id="contactform">
<div class="one_half">
<input name="name" type="text" id="name" size="30"
onfocus="if(this.value === 'Name') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Name'; }"
value="Name">
<input name="alter" type="text" id="alter" size="3"
onfocus="if(this.value === 'Alter') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Alter'; }"
value="Alter">
<input name="email" type="text" id="email" size="30"
onfocus="if(this.value === 'E-Mail') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'E-Mail'; }"
value="E-Mail">
<input name="phone" type="text" id="phone" size="30"
onfocus="if(this.value === 'Handynummer') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Handynummer'; }"
value="Handynummer">
<input name="facebook" type="text" id="facebook" size="200"
onfocus="if(this.value === 'Facebook') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Facebook'; }"
value="Facebook">
<input name="instagram" type="text" id="instagram" size="200"
onfocus="if(this.value === 'Instagram') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Instagram'; }"
value="Instagram">
</div>
<div class="one_half last">
<textarea name="comments" cols="40" rows="3" id="comments"
onfocus="if(this.value === 'Nachricht') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Nachricht'; }">
Nachricht
</textarea>
</div>
<input type="submit" class="send_message" id="submit" value="Senden"/>
</form>
<?php echo $mailResult ?>
</body>
</html>
我無論如何都會避免這種解決方案。 我會用 AJAX 來解決這個問題。 在這些解決方案中,我們將邏輯與渲染混合在一起。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.