0

PHP Code :

<?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>

Source Code :

<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>

Problem

I want to clear all input text after sending a mail on submit.on submit I am calling cleartext() from my PHP code.but on that time I am not being able to clear my input text value.And when I call cleartext() method on clear button it's works proper.if somebody have idea about it so please help me.

Govinda Rajbhar
  • 2,778
  • 3
  • 32
  • 58

5 Answers5

0

You missed script type. that might be the problem.

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

see this How to call a JavaScript function from PHP?

Community
  • 1
  • 1
MjZac
  • 3,060
  • 1
  • 17
  • 24
  • sorry buddy but it's not issue of `type="text/javascript"`.my code is also running but my problem is that i am not being able to clear value of input text. – Govinda Rajbhar Jan 27 '14 at 08:12
0

For button to clear fields, use type "reset" instead of "submit"

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

And to clear the fields after submit, try redirecting

if($sent) {
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit(0);
}
Mario Radomanana
  • 1,660
  • 1
  • 22
  • 30
0

you can put a setTimeout in the onClick of the js function that clear the form

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

you using submit button for clear, it shouldn't work because the form will be submitting its values
and the way is you should make it return false in onclick in order to prevent submitting form.
your code should be like this:

<input type="submit" id="clear" name="clear" class="button" value="Clear &nbsp;&nbsp;&nbsp;" onClick="cleartext(); return false;">
Josh Crozier
  • 202,159
  • 50
  • 343
  • 273
Eko Dedy
  • 379
  • 6
  • 18
-1

Try this:

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();
hindmost
  • 6,828
  • 3
  • 23
  • 36