是否可以使用php重置表单而不刷新? 我该如何存档?
或者可以用js吗?
最重要的是不刷新页面而不为此添加另一个按钮(这是因为设计)
任何想法? 谢谢 我正在我的表格上使用recaptcha,一旦我提交了我想发送信息的信息 这是我的js代码
function validateCaptcha(){
challengeField = $("input#recaptcha_challenge_field").val();
responseField = $("input#recaptcha_response_field").val();
nameField = $("input#name").val();
emailField = $("input#email").val();
phoneField =$("input#phone").val();
reasonField =$("select#reason").val();
messageField =$("textarea#message").val();
var html = $.ajax({
type: "POST",
url: "ajax.recaptcha.php",
data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField +"&name="+ nameField +"&email=" + emailField +"&phone="+ phoneField + "&reason=" + reasonField +"&message=" + messageField,
async: false
}).responseText;
if(html == "success")
{
$("#captchaStatus").html("Success. Submitting form.");
$("#thanks").html("Thank you, we going to keep in touch with you soon. ");
return false;
}
else
{
$("#captchaStatus").html("Your captcha is incorrect. Please try again");
Recaptcha.reload();
return false;
}
}
这是我的PHP代码
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$reason = $_POST['reason'];
$message = $_POST['message'] ;
if (empty($name)|| empty($email) || empty($message))
{
}
else
{
$resp = recaptcha_check_answer (PRIVATEKEY, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
$header = 'From: ' . $email . " \r\n";
$msg = "Sent from: " . $name . "\r\n";
$msg .= "Email: " . $email . " \r\n";
$msg .= "Phone: " . $phone . " \r\n";
$msg .= "Contact reason:" . $reason . " \r\n";
$msg .= "Message: " . $message . " \r\n";
$to = 'patricia@anetdesign.com';
$subject = 'Emailmakers contact page';
mail($to, $subject, utf8_decode($msg), $header);
?>success<?
}
else
{
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
}
}
答案 0 :(得分:2)
http://ilovethecode.com/Javascript/Javascript-Tutorials-How_To-Easy/Javascript_Reset_Form.shtml
此链接可以帮助您
答案 1 :(得分:1)
好吧,一旦你发现ajax请求,如果验证码失败,那么只需重新加载整个表单。
假设您只有myform.php中的表单,并且您将其包含在主页面中,当验证码不匹配时,您只需重新加载表单即可。像这样:
if (html != "Success")
{
$("#divThatHoldsTheForm").html(html)
}
在你的myform.php中你只有:
<form action="..." method="POST">
<!-- some inputs -->
</form>
答案 2 :(得分:0)
使用reset()
功能。如果您有一个名为frm
的表单,则可以使用frm.reset()
。
或者只是编写代码以首先获取所有文本框并将其value
属性设置为""
。这不需要刷新你的页面
答案 3 :(得分:0)
使用PHP意味着刷新页面(仅在ajax的情况下)所以我建议你尝试使用javascript而不是php来完成这项任务