-2
我想向我的網站上的聯繫表單添加另一個輸入字段。默認表單附帶名稱,電子郵件和消息字段。我正在嘗試添加主題字段。php郵件表單中的字段沒有註冊用戶輸入
問題是它不會在主題字段中記錄任何輸入。因此,當我收到電子郵件時,所有默認字段都已正確記錄了輸入,但我添加的主題字段爲空。
下面是相關的HTML,JavaScript的(阿賈克斯)和PHP代碼:
//Listens for Submit button, prevents page refresh and calls on submitForm()
$("#contactForm").submit(function(event) {
// cancels the form submission
event.preventDefault();
submitForm();
});
//Grabs form input, and starts an ajax object
function submitForm() {
// Initiate Variables With Form Content
var name = $("#name").val();
var email = $("#email").val();
var messagesubject = $("#messagesubject").val();
var message = $("#message").val();
//I have added "&messagesubject=" + messagesubject to the data: to get input in the subject field
$.ajax({
type: "POST",
url: "php/form-process.php",
data: "name=" + name + "&email=" + email + "&messagesubject=" + messagesubject + "&message=" + message,
success: function(text) {
if (text == "success") {
formSuccess();
}
}
});
}
function formSuccess() {
$("#msgSubmit").removeClass("hidden");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.2/js/bootstrap-select.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/css/bootstrap-select.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-lg-6 col-xs-12">
<div class="contact-form">
<h3>Send us a message. We're here to help!</h3>
<form role="form" id="contactForm">
<div class="row">
<div class="form-group col-sm-6">
<label for="name" class="h4">Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter name" required>
</div>
<div class="form-group col-sm-6">
<label for="email" class="h4">Email</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" required>
</div>
</div>
<div class="form-group">
<label for="messagesubject" class="h4 ">Subject</label>
<input type="messagesubject" class="form-control" placeholder="Enter Subject" required>
</div>
<div class="form-group">
<label for="message" class="h4 ">Message</label>
<textarea id="message" class="form-control" rows="5" placeholder="Enter your message" required></textarea>
</div>
<button type="submit" id="form-submit" class="btn btn-success btn-lg pull-right ">Submit</button>
<div id="msgSubmit" class="h3 text-center hidden">Message Submitted!</div>
</form>
</div>
</div>
這裏是PHP代碼來處理phpmail和獲取信息到我的郵箱。不能將它包含在代碼片段中,但它是一個非常簡單的phpmail設置。
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$MessageSubject = $_POST["MessageSubject"];
$message = $_POST["message"];
$EmailTo = "[email protected]";
$Subject = "$messagesubject from $name";
// prepare email body text
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $messagesubject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, .$MessageSubject "From:".$email);
// redirect to success page
if ($success){
echo "success";
}else{
echo "invalid";
}
?>
doh!謝謝!! – raimond
不客氣。祝好運與其餘。 – Robbie