我有一個PHP聯繫表單,目前將您帶到一個單獨的頁面。我希望「成功/錯誤消息」出現在jQuery對話框中,而不是將您重定向到另一個頁面。或者,如果我可以將它定位到DIV並在提交時顯示它,我只需要在iframe以外的同一頁面上顯示結果。如何在jQuery對話框中顯示PHP結果
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Message From Testing.com";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<html>
<div id="dialog" title="Thank You">
<p>Thank you for contacting us. We will be in touch with you very soon.</p>
</div>
</html>
<?php
}
?>
這是我的HTML。
<form name="contactform" method="get" autocomplete="on">
<input type="text" class="contact-form- input" name="name" placeholder="Name">
<input type="text" class="contact-form-input" name="email" placeholder="Email">
<input type="text" class="contact-form-input" name="telephone" placeholder="Phone">
<textarea rows="5" cols="50" class="contact-form-textarea" name="message" placeholder="Message"></textarea>
<input type="button" class="form-submit-button" name="send" onclick="mail();" value="Send">
<input type="reset" class="form-submit-button" name="clear" value="Clear">
</form>
你可以添加HTML和JQuery嗎? php文件不是必需的 – Netzach
@Netzach我剛剛寫完一個評論,詢問同樣的事情。 – NobleUplift