I’m making a plugin on WordPress to send emails to our clients. I have a trouble with a field array type of form, because i can’t get it on PHP function. I can get single fields, as subject or message, but not email field (it’s an array creted dynamically for clients that have multiple emails)
Here my form code
echo '<form id="myform" method="post">';
echo "<ul>";
echo "<li><strong>¿Cual es el destinatario del email? (pueden ser varios)</strong></li>";
if( get_field('contactos',$post_id) ):
while( has_sub_field('contactos',$post_id) ):
echo "<li style='margin-left: 45px; margin-top: 15px;'><input type='checkbox' id='emails[]' value='".get_sub_field('email',$post_id)."'>"
.get_sub_field('nombre',$post_id)." (".get_sub_field('email',$post_id)." )</li>";
endwhile;
endif;
echo '<li style="margin-top: 15px;"><strong><label for="subject">Asunto: </label></strong>';
echo '<input id="subject" maxlength="45" size="30" name="subject"></li>';
echo "<p>Texto del mensaje</p>";
echo '<textarea id="cuerpo" class="textarea" name="cuerpo" placeholder="" style="
width: 600px;
height: 200px;
"></textarea><br>';
echo '<li style="margin-top: 35px;"><input id="send" name="submit" type="submit" value="Enviar eMail" class="button button-primary button-large"></li>';
echo "</form>";
Here ajax.js
$( "#myform" ).submit(function( event ) {
event.preventDefault();
alert( "Handler for .submit() called." );
$(function() {
$('#sendingemails').fadeIn();
var subject = $("#subject").val();
var cuerpo = $("#cuerpo").val();
$.ajax({
type: 'POST',
url: GestionAjax.ajaxurl,
data: { action: 'email', subject : subject, cuerpo : cuerpo, emails : $('#emails').serialize() },
// Mostramos un mensaje con la respuesta de PHP
success: function(data) {
alert("enviado");
$('#sendingemails').fadeOut();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
alert (jsonString);
}
})
});
});
and here the function that recive data:
function AC_ONEmail() {
$emails = array();
$emails = $_POST['emails'];
$message = $_POST['cuerpo'];
$subject = $_POST['subject'];
add_action('phpmailer_init','send_smtp_email');
$headers[] = 'From: '.$current_user->display_name.' '.$current_user->user_email;
add_filter( 'wp_mail_content_type', 'my_custom_email_content_type' );
$message .= '<br><br>'.nl2br($current_user->firma);
foreach ($emails as $email) {
$result=wp_mail( $email, $subject, $message, $headers);
if (!$result) {
global $ts_mail_errors;
global $phpmailer;
if (!isset($ts_mail_errors)) $ts_mail_errors = array();
if (isset($phpmailer)) {
$ts_mail_errors[] = $phpmailer->ErrorInfo;
}
} else {
global $wpdb;
$wpdb->insert(
'wp_comunicaciones',
array(
'post_id' => $idcliente,
'asunto' => $datos2,
'cuerpo' => $message,
'fecha' => date("Ymd"),
'responsable' => $current_user->display_name,
'email' => $email[0]
)
);
/*envio del email a una carpeta del servidor
$mail_string = "From: ".$current_user->user_email."rn". "To: ".$value."rn"."Subject: ".$_POST['subject']."rn"."rn".$_POST['cuerpo']."rn";
$stream = imap_open("{smtp.office365.com}Elementos enviados", $current_user->cuenta_usuario, $current_user->passwordSMTP);
imap_append($stream, "{smtp.office365.com}Elementos enviados", $mail_string, "\Seen");
*/
}
}
$arr = array( "asunto" => $_POST['subject'], "cuerpo" => $_POST['cuerpo'], "emails" => $_POST['emails']);
echo json_encode($arr);
wp_die();
}
As i said, i debug vars on console (network tab on chrome), and subject and message have values, but don’t clientes (an array type field)
I see a lot of info here, on stackoverflow, i try to do by different ways, but i spend two days on it, and now i need your knowledge. How can i do it?
Thanks
UPDATE SOLUTION
I take a solution
On ajax script i change data line with this data: { action: 'email', datos : $('#myform').serialize() }
, and on function that recive data i insert parse_str($_POST['datos'], $arraydatos);
Then i take data with $arraydatos['emails']
or field that i want. It works fine!
I take a solution to do it
I change ajax.js with this
and on function that recive data i change by this
I explain changes. I send all form serialized to php function that recive data. On PHP function i read data and parse it to an array
parse_str($_POST['datos'], $arraydatos);
.Then i can get all data with
$arraydatos['form_field']
, and use it, including form array fields.For content i use a wp_editor function. To get it on PHP function i include on ajax
tinyMCE.triggerSave()
. Without it, ‘cuerpo’ field was emptyThanks