gpt4 book ai didi

javascript - 发送 AJAX 联系表后如何清除字段?

转载 作者:太空宇宙 更新时间:2023-11-04 16:21:35 24 4
gpt4 key购买 nike

先上代码:

CONTACT_FORM.HTML

<html>
<head>
<title>Contact Form</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="contact_form.css" />
<script src="contact_form.js"></script>
</head>
<body>
<div id="mainform">
<form id="form" name="form" action="contact_form.html">
<h3>Contact Form</h3>
<p id="returnmessage"></p>
<label>Name: <span>*</span></label>
<input type="text" id="name" placeholder="Name"/>
<label>Email: <span>*</span></label>
<input type="text" id="email" placeholder="Email"/>
<label>Contact No: <span>*</span></label>
<input type="text" id="contact" placeholder="10 digit Mobile no."/>
<label>Message:</label>
<textarea id="message" placeholder="Message......."></textarea>
<input type="button" id="submit" value="Send Message"/>
</form>
</div>
</body>
</html>

CONTACT_FORM.PHP

<?php
$name = $_POST['name1'];
$email = $_POST['email1'];
$message = $_POST['message1'];
$contact = $_POST['contact1'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (!preg_match("/^[0-9]{10}$/", $contact)) {
echo "<span>* Please Fill Valid Contact No. *</span>";
} else {
$subject = "Message from website...";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n";
$headers .= 'Cc:' . $email. "\r\n";
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>Thank you for contacting us.<br/><br/>'
. 'Name: ' . $name . '<br/>'
. 'Email: ' . $email . '<br/>'
. 'Contact No: ' . $contact . '<br/>'
. 'Message: ' . $message . '<br/><br/>'
. 'This is a contact confirmation email.'
. '<br/>'
. 'We will keep you posted with our goings on.</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";
$sendmessage = wordwrap($sendmessage, 70);
mail("me@mydomain.com", $subject, $sendmessage, $headers);
echo "Thank you, your query has been received.";
}
} else {
echo "<span>* invalid email *</span>";
}
?>

CONTACT_FORM.JS

$(document).ready(function() {
$("#submit").click(function() {
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
var contact = $("#contact").val();
$("#returnmessage").empty();
if (name == '' || email == '' || contact == '') {
alert("Please Fill Required Fields");
} else {
$.post("contact_form.php", {
name1: name,
email1: email,
message1: message,
contact1: contact
}, function(data) {
$("#returnmessage").append(data);
if (data == "Your Query has been received, We will contact you soon.") {
// $("#form")[0].reset();
$('#form').find('form')[0].reset();
}
});
}
});
});

我的问题是这些......

  1. 提交后如何清除表单?
  2. 为什么无法将 CC 返回给发件人?

提前致谢。

最佳答案

至于重置,您的代码正在尝试查看一种形式以找到另一种形式......不存在

改变

 $('#form').find('form')[0].reset();

 $('#form')[0].reset();

关于javascript - 发送 AJAX 联系表后如何清除字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28857003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com