dsebao
9/11/2015 - 2:22 PM

Using Ajax in WordPress

Using Ajax in WordPress #WordPress

jQuery(document).ready(function($) {
  
    $("form#sendform").submit(function(event) {
      event.preventDefault();

      var $form = $(this);

      if($('#email',this).val()){

        $.ajax({
          type: "POST",
          url: "wp-admin/admin-ajax.php",
          data: $form.serialize(),
          beforeSend: function (data) {
          },
          success: function(data){
            if(data){
             
            } else {
              
            }
          }
        });
      } else {
        console.log('error');
      }
    });
});
<form id="sendform" method="post">
        <textarea name="sugerencias" id="sug" class="form-control"></textarea>
        
        <input type="email" class="form-control" id="email" value="" required>
        <input type="hidden" name="action" value="sendform">

        <button type="submit" class="btn btn-primary">Enviar</button>
        <span class="mensaje"></span>
</form>
//Send email
function sendform(){
  // do stuff
	die();
}

add_action('wp_ajax_sendform', 'sendform');
add_action('wp_ajax_nopriv_sendform', 'sendform');