0

I know that my question was asked many other times and I read all discussions about that but I don't have found the right answer.

My problem is that admin-ajax request with ajax call return always 0 on wordpress. My codes are:

Front End call

$.ajax({
   url: cubetechform_ajax.ajax_url,
   type: "POST",
    data: {
        action: 'cubetech_contact_form_send_email',
        name: name,
        phone: phone,
        email: email,
        message: message
    },
    cache: false,
    success: function(response) {
        console.log(response);
    },
    error: function(XMLHttpRequest, ajaxOptions, thrownError) {                     
        //console.log(XMLHttpRequest.status + '<br/>' + ajaxOptions + '<br/>' + thrownError);               
    },
});

Back End

/*
 * Action hooks
 */
function __construct() {

    // Enqueue plugin styles and scripts
    add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_cubetech_form_scripts' ) );

    // Setup Ajax action hook
    add_action( 'wp_ajax_cubetech_send_email', array( $this, 'cubetech_send_email' ) );
    add_action( 'wp_ajax_nopriv_cubetech_send_email', array( $this, 'cubetech_send_email' ) );

    //$this->cubetech_send_email();
}

/**
 * Register plugin styles and scripts
 */
public function register_contactme_scripts() {
    wp_register_script( 'contactme_script', get_template_directory_uri() . '/js/contact_me.js', array('jquery','bootstrapValidationScript'), '', true);
}   
/**
 * Enqueues plugin-specific scripts.
 */
public function enqueue_cubetech_form_scripts() {       
    wp_enqueue_script( 'contactme_script', plugins_url( '/js/contact_me.js', __FILE__ ), array('jquery','bootstrapValidationScript'), '', true);
    //wp_enqueue_script( 'contactme-script' );
    wp_localize_script( 'contactme_script', 'cubetechform_ajax', array( 'ajax_url' => admin_url('admin-ajax.php')) );
}   


public function cubetech_send_email() { 
    require_once ABSPATH . WPINC . '/class-phpmailer.php'; 
    require_once ABSPATH . WPINC . '/class-smtp.php';

    $name = strip_tags(htmlspecialchars($_POST['name']));
    $email_address = strip_tags(htmlspecialchars($_POST['email']));
    $phone = strip_tags(htmlspecialchars($_POST['phone']));
    $message = strip_tags(htmlspecialchars($_POST['message']));

    // definisco il messaggio formattato in HTML
    $email_body = '<html><body>';
    $email_body .= '<p>Nome: '.$name.'<br>';
    $email_body .= 'N. Telefono: '.$phone.'<br>';
    $email_body .= 'Richiesta: '.$message.'</p>'.'<br>';
    $email_body .= '</body></html>';

    // definisco il messaggio formattato in HTML
    $email_body = '<html><body>';
    $email_body .= '<p>Nome: '.$name.'<br>';
    $email_body .= 'N. Telefono: '.$phone.'<br>';
    $email_body .= 'Richiesta: '.$message.'</p>'.'<br>';
    $email_body .= '</body></html>';

    $to = get_option('admin_email');
    $subject = "Richiesta da: ". $email_address;

    $mail = new PHPMailer();

    $mail->IsSMTP(); // set mailer to use SMTP
    $mail->Host = "smtps.aruba.it";  // specify main and backup server
    //$mail->SMTPDebug = 3;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;     // turn on SMTP authentication
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;
    $mail->Username = "info@cubetech.it";  // SMTP username
    $mail->Password = "*******"; // SMTP password

    $mail->From = "info@cubetech.it";
    $mail->FromName = "Cube Tech Bologna";
    $mail->AddAddress("info@cubetech.it", "Cube Tech Bologna" );  // name is optional
    $mail->AddReplyTo("info@cubetech.it", "Reply");
    $mail->AddCC($email_address); 

    $mail->IsHTML(true);  // set email format to HTML

    $mail->SetFrom("info@cubetech.it", "Cube Tech Bologna");
    $mail->Subject = $subject;
    $mail->Body = $email_body;

    $mail->AddAddress("info@cubetech.it");

    $error = '';

    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        echo $error;
        exit();
        die();
    } else {
        $error = 'ok';
        echo $error;
        exit();
        die();
    }
}

I tried everything I read about die(); exit(); $mail = new PHPMailer(); $mail = new PHPMailer(true); and so much other. The problem is always with ajax call because if I try to call direct the function cubetech_send_email() it's work perfectly and return ok and phpmailer send it. I try all this on localhost with server email and all works perfect.. Someone can help me to resolve it? Thank you

LW001
  • 2,452
  • 6
  • 27
  • 36
C. Max
  • 45
  • 1
  • 7

2 Answers2

0

Your hooks should be like so:

// Setup Ajax action hook
add_action( 'wp_ajax_cubetech_contact_form_send_email', array( $this, 'cubetech_send_email' ) );
add_action( 'wp_ajax_nopriv_cubetech_contact_form_send_email', array( $this, 'cubetech_send_email' ) );

In your data object when doing the ajax refers to "cubetech_contact_form_send_email" as the action. The hooks works like so: wp_ajax_(action) https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

fppz
  • 409
  • 2
  • 9
  • Your observation is right but the problem continues... I fix with your suggestions but never change – C. Max Jun 08 '17 at 16:01
0

After many and many days where I tried everythings... everycode, where I have read all posts and solutions, I have found solution... there wasn't any error in code... all was good. The problem was when I passed localhost wordpress to hosting. I don't know why but maybe something was no went well during this process... I erase database and directory on hosting and reinstalled everything... I hope this can help the user that have this problem

C. Max
  • 45
  • 1
  • 7