1

I'm trying to send an e-mail with contact information through Wordpress's native PHPMailer class and Ajax. Here's the relevant code:

jQuery(document).ready(function() {
  jQuery('#formContato').submit(ajaxSubmit);

  function ajaxSubmit() {
    var newCustomerForm = jQuery('#formContato').serialize();

    jQuery.ajax({
      type: "POST",
      url: "<?php echo site_url(); ?>/wp-admin/admin-ajax.php",
      data: {
        action: 'sendmail',
        newCustomerForm
      },
      success: function(data) {

        jQuery("#alertaOk").show();
        console.log(data);
        console.log(newCustomerForm);
      },
      error: function(errorThrown) {

        jQuery("#alertaErro").show();
        console.log(errorThrown);
      }
    });
    return false;
  }
});
#alertaOk {
  display: none;
}

#alertaErro {
  display: none;
}
<form id="formContato" method="post">
  <div class="row">
    <div class="col-sm-6 form-group">
      <input class="form-control" id="name" name="name" placeholder="Nome" type="text" required>
    </div>
    <div class="col-sm-6 form-group">
      <input class="form-control" id="email" name="email" placeholder="E-mail" type="email" required>
    </div>
  </div>
  <textarea class="form-control" id="comments" name="comments" placeholder="Mensagem" rows="5"></textarea><br>
  <div class="row">
    <div class="col-sm-12 form-group">
      <button class="btn btn-default pull-left btn-laranja" type="submit">Enviar</button>
    </div>
  </div>
  <div id="alertaOk" class="alert alert-success">
    <strong>Obrigado!</strong> Recebemos sua mensagem e entraremos em contato em breve.
  </div>
  <div id="alertaErro" class="alert alert-danger">
    <strong>Algo deu errado.</strong> Nossos administradores já foram avisados, por favor envie sua mensagem para o e-mail <a href="[email protected]">[email protected]</a> .
  </div>
</form>

The Ajax sends the data to a function in Wordpress's functions.php file that calls PHPMailer. Before initializing PHPMailer I'm retrieving the ajax data like this:

$nome = trim($_POST['name']);
$mensagem = trim($_POST['comments']);
$email = trim($_POST['email']);

and then trying to print it into the email's body:

    $mail->Body = "Nome: " . $nome . "\nE-mail: " . $email . "\nMensagem: " . $mensagem;

PHP executes the functions just right, I receive the test emails, but it's not receiving the data from the Ajax call. The console is showing the serialized data, which means that it's not wrong, but somehow it's not reaching the PHP script. I suspect it's something related to Wordpress.

Thanks in advance.

2

2 Answers 2

1

Your newCustomerForm needs to have a key in data:

data: {
   action: 'sendmail',
   custForm: newCustomerForm
}

And in your PHP file you will need to parse the custForm string:

parse_str($_POST['custForm'], $form);
//Then access the custForm data using $form, e.g. $form['name']
Sign up to request clarification or add additional context in comments.

1 Comment

you're on the money with parse_str, however the data: object was fine as written, he doesn't need to give newCustomerForm a key.
0

You can't pass two different type of 'parameters' at the same time, either you pass the serialized data or you pass it by each element. Put the action into the newCustomerForm before you serialize it. So

 data: {
    action: 'sendmail',
    newCustomerForm
  },

is

 data: newCustomerForm,

2 Comments

This is incorrect, you can have many parameters.The only problem is that newCustomerForm doesn't have a key. As @Funk Doc commented.
newCustomerForm is an serialized array, so wouldn't need a key. But he's trying to mix a single entry with an array and address it as an array. He needs to change either the way he passes the data or change the php on how he reads it. I went for the Javascript way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.