title: How do you clear form field after submit/success?
body: <p>I'm working with this exact same MailChimp PHP/AJAX subscribe processing form - <a href="https://stackoverflow.com/questions/10403927/clear-text-value-after-success">clear text value after success?</a> - but I am absolutely stumped on how to actually get the form field to cleat after success.</p>
  
  <p>Apparently to what Felix recommended here - <a href="https://stackoverflow.com/questions/10403927/clear-text-value-after-success">clear text value after success?</a> -  the code works only if the span element exists at document load, and you have to call the if-statement after completion if using Ajax.</p>
  
  <p>Could someone please help me? How do you clear the form field after it has been successfully completed by a user?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>    &lt;?php  
      // store-address.php
  
      function storeAddress(){
  
          // Validation
          if(!$_GET['email']){ return "No email address provided"; } 
  
          if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
              return "Email address is invalid"; 
          }
  
          require_once('MCAPI.class.php');
          // grab an API Key from http://admin.mailchimp.com/account/api/
          $api = new MCAPI('my-api-key');
  
          // grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
          // Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
          $list_id = "my-list";
  
          if($api-&gt;listSubscribe($list_id, $_GET['email'], '') === true) {
              // It worked!   
              return 'Success! Check your email to confirm sign up.';
          }else{
              // An error ocurred, return error message   
              return 'Error: ' . $api-&gt;errorMessage;
          }
  
      }
  
      // If being called via ajax, autorun the function
      if($_GET['ajax']){ echo storeAddress(); }
      ?&gt;
  </code></pre>
  
  <p>The form code:</p>
  
  <pre><code>    &lt;form action="&lt;?=$_SERVER['PHP_SELF']; ?&gt;" id="signup" class="subscribe" method="get"&gt;
  
          &lt;fieldset&gt;
              &lt;input type="text" id="email" name="email" placeholder="your email address" /&gt;
              &lt;input type="submit" name="submit" value="Send it" /&gt;
          &lt;/fieldset&gt;
      &lt;/form&gt;
      &lt;p id="response"&gt;&lt;? require_once('inc/store-address.php'); if($_GET['submit']){ echo storeAddress(); } ?&gt;&lt;/p&gt;
      &lt;script type="text/javascript" src="/js/prototype.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="/js/mailing-list.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>The mailing-list javascript code:</p>
  
  <pre><code>    // Load Events Listeners
      Event.observe(window, 'load', init, false);
  
      function init(){
          Event.observe('signup','submit',storeAddress);
      }
  
      // AJAX call sending sign up info to store-address.php
      function storeAddress(event) {
          // Update user interface
          $('response').innerHTML = 'Getting email address...';
          // Prepare query string and send AJAX request
          var pars = 'ajax=true&amp;email=' + escape($F('email'));
          var myAjax = new Ajax.Updater('response', 'inc/store-address.php', {method: 'get', parameters: pars});
          Event.stop(event); // Stop form from submitting when JS is enabled
      }
  </code></pre>