PHP代码:
<?php
if(isset($_POST['submit']))
{
$sent = mail($to, $subject, $email_message, $headers) ;
if($sent)
{
echo '<script> cleartext(); </script>';
}
}
?>
JavaScript:
<script type="text/javascript">
function cleartext()
{
alert(document.getElementById('name').value);
document.getElementById('name').value = '';
document.getElementById('email').value = '';
document.getElementById('phone').value = '';
document.getElementById('message').value = '';
alert(document.getElementById('name').value);
}
</script>
源代码:
<form action="#" method="post" class="margin-bottom" target="hidden" id="contact">
<div class="row">
<div class="col-lg-6 col-sm-6">
<input type="text" placeholder="Your Name*" id="name" name="name" />
</div>
<div class="col-lg-6 col-sm-6">
<input type="text" placeholder="Your Email*" id="email" name="email" />
</div>
<div class="col-lg-6 col-sm-6">
<input type="text" placeholder="Your Phone*" id="phone" maxlength="13" name="phone" />
</div>
</div>
<textarea placeholder="Message" id="message" name="message"></textarea>
<input type="submit" id="clear" name="clear" class="button" value="Clear " onClick="cleartext();">
<input name="submit" type="submit" class="button" id="submit" onClick="MM_validateForm('name','','R','email','','RisEmail','phone','','RisNum','message','','R');return document.MM_returnValue" value="Submit ">
</form>
问题
我希望在提交后发送邮件后清除所有输入文本。提交我从cleartext()
代码中调用PHP
。但那时我无法清除输入文本值得。当我在清除按钮上调用cleartext()
方法时,它正常工作。如果有人对此有所了解,那么请帮助我。
答案 0 :(得分:0)
你错过了脚本类型。这可能是问题。
<?php
//stuff to do
echo '<script type="text/javascript">
cleartext();
</script>';
?>
答案 1 :(得分:0)
要使按钮清除字段,请使用“重置”类型而不是“提交”
<input type="reset" value="Clear" />
要在提交后清除字段,请尝试重定向
if($sent) {
header('Location: ' . $_SERVER['PHP_SELF']);
exit(0);
}
答案 2 :(得分:0)
您可以在清除表单的js函数的onClick中放置一个setTimeout
<input name="submit" type="submit" class="button" id="submit" onClick="setTimeout( MM_validateForm, 1000 )" >
答案 3 :(得分:-1)
您使用提交按钮清除,它不应该工作,因为表单将提交其值
并且你应该在false
中让它返回onclick
以防止提交表格
你的代码应该是这样的:
<input type="submit" id="clear" name="clear" class="button" value="Clear " onClick="cleartext(); return false;">
答案 4 :(得分:-1)
试试这个:
PHP:
if(isset($_POST['submit']))
{
$sent = mail($to, $subject, $email_message, $headers) ;
if($sent) $b_sent = true;
}
使用Javascript:
function cleartext()
{
alert(document.getElementById('name').value);
document.getElementById('name').value = '';
document.getElementById('email').value = '';
document.getElementById('phone').value = '';
document.getElementById('message').value = '';
alert(document.getElementById('name').value);
}
var bClear = <?= $b_sent? 'true':'false' ?>;
if (bClear) cleartext();