我想要做的是將提交的名稱、電子郵件和訊息發送到我的 php 腳本,然后發送電子郵件。問題是我的表單操作沒有觸發,而是重新加載頁面。
更新 不知道我的 html 表單中缺少什么:
<form method="post" action="">
<div class="input-group">
<input type="text" id="name" name="name" class="input-demo" placeholder="Your Name">
<span id="invalid-name">
Please enter at least 2 chars
</span>
</div>
<div class="input-group">
<input id="email" type="email" name="email" class="input-demo" placeholder="Email Address">
<span id="invalid-email">
Please enter valid email
</span>
</div>
<div class="input-group">
<textarea id="message" name="message" placeholder="Message">
</textarea>
<span id="invalid-message">
Please write something for us
</span>
</div>
<div>
</div>
<input type="submit" name="submit" value="Book a Demo">
</form>
UPDATE
php 腳本首先獲取值并構造電子郵件,然后最終發送:
<?php
if (isset($_POST['submit'])) {
include 'index.php';
$to = "[email protected]"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
} else {
echo 'isset was false';
}
?>
這是我的檔案夾結構

我在 localhost ubuntu apache 服務器中運行它。
uj5u.com熱心網友回復:
您的 HTML 對于一件小事來說很好,您沒有指定在點擊提交后將表單發送到哪里。您可以通過在action引數中指定 PHP 腳本來執行此操作<form>
<form method="post" action="form-to-email.php">
<div class="input-group">
<input type="text" id="name" name="name" class="input-demo" placeholder="Your Name">
<span id="invalid-name">
Please enter at least 2 chars
</span>
</div>
<div class="input-group">
<input id="email" type="email" name="email" class="input-demo" placeholder="Email Address">
<span id="invalid-email">
Please enter valid email
</span>
</div>
<div class="input-group">
<textarea id="message" name="message" placeholder="Message">
</textarea>
<span id="invalid-message">
Please write something for us
</span>
</div>
<div>
</div>
<input type="submit" name="submit" value="Book a Demo">
</form>
在閱讀了其他人回答的問題后,我終于找到了您想要的內容,您可以通過在腳本末尾添加標題(位置:index.html)來阻止網站停留在您的 phpscript 頁面上,這樣當它完成時注冊或失敗時將其發送回索引。或者,您可以在索引檔案中包含 PHP,但您必須將索引從更改.html為.php
在您的 php 腳本中使用重定向的示例
<?php
if (isset($_POST['submit'])) {
include 'index.php';
$to = "[email protected]"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
header('Location: index.html');
} else {
echo 'isset was false';
header('Location: index.html');
}
?>
uj5u.com熱心網友回復:
您粘貼的表單元素的 action 屬性未設定:
<form method="post" action="">
如果此屬性未設定或為空,表單將提交到當前頁面,因為瀏覽器無法知道將其提交到哪里。假設 form-to-email.php 是要處理的腳本,它應該如下所示:
<form method="post" action="form-to-email.php">
或者,您可以通過將 index.html 重命名為 index.php(不會執行 .html 檔案中的 PHP 代碼)并從那里執行 PHP 腳本來訪問 HTML 頁面中的 PHP 代碼,如下所示:
<?php
include "form-to-email.php";
?>
這應該允許您將操作屬性留空并且仍然具有功能形式。
uj5u.com熱心網友回復:
您需要將欄位名稱從“名稱”更改為任何其他單詞..
試試下面的代碼:
<input type="text" id="name" name="full_name" class="input-demo" placeholder="Your Name">
謝謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/506642.html
上一篇:在顏色面板上拖動時更新顏色
