javascript 执行某些PHP代码后清除输入文本

tcomlyy6  于 2023-03-11  发布在  Java
关注(0)|答案(5)|浏览(110)

PHP代码:

<?php
    if(isset($_POST['submit']))
    {
       $sent = mail($to, $subject, $email_message, $headers) ;

       if($sent)
       {
        echo '<script> cleartext(); </script>';
       }

    }
?>

脚本语言:

<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 &nbsp;&nbsp;&nbsp;" 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 &nbsp;&nbsp;&nbsp;">

</form>

问题

我想清除所有输入文本发送邮件后提交。在提交我调用PHP从我的PHP代码。但在这一次我不能清除我的输入文本值。当我调用cleartext()方法上清除按钮它的工作正常。如果有人有关于它的想法,所以请帮助我。

eulz3vhy

eulz3vhy1#

你错过了脚本类型。这可能是问题所在。

<?php
 //stuff to do
 echo '<script type="text/javascript"> 
 cleartext();  
 </script>';
?>

请参见此How to call a JavaScript function from PHP?

xqk2d5yq

xqk2d5yq2#

对于清除字段的按钮,使用类型“reset”代替“submit”

<input type="reset" value="Clear" />

要在提交后清除字段,请尝试重定向

if($sent) {
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit(0);
}
i2byvkas

i2byvkas3#

您可以在清除表单的js函数的onClick中放置setTimeout

<input name="submit" type="submit" class="button" id="submit" onClick="setTimeout( MM_validateForm, 1000 )" >
ki0zmccv

ki0zmccv4#

试试这个:
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();
zazmityj

zazmityj5#

您使用提交按钮清除,它不应该工作,因为表单将提交其值
方法是让它在onclick中返回false,以防止提交表单。
你的代码应该是这样的:

<input type="submit" id="clear" name="clear" class="button" value="Clear &nbsp;&nbsp;&nbsp;" onClick="cleartext(); return false;">

相关问题