I got a PHP variable called $author_email which I want to pass to a Javascript function which I call via HTML on button clickonclick="searchEmail();" Can I pass the variable to Javascript but doing this instead? onclick="searchEmail('<?php echo $author_email?>');"
Then I want to use this in my javascript function to pass it to the PHP function it calls via AJAX.
This is the Javascript function searchEmail:
function searchEmail() {
var admin_url = admin_ajax.ajaxurl;
$.ajax({
type: "POST",
url: admin_url,
datatype: "html",
data: {'action': 'search_notify_email'},
success: function() {
searchNotification();
},error:function() {
searchNotificationError();
}
});
}
The javascript fires the PHP function search_notify_email
I now want to pass the variable to the PHP function. How is this done?
Which looks like this:
function search_notify_email(){
$to = $author_email;
$subject = "Test title";
$message = "message message message message message message message ";
$headers[] = 'From: Test <[email protected]>';
if( wp_mail($to, $subject, $message, $headers) ){
// Success
} else {
// Error
}
die();
}
add_action('wp_ajax_nopriv_search_notify_email', 'search_notify_email');
add_action('wp_ajax_search_notify_emaill', 'search_notify_email');
As you can see my goal is to use the $author_email in the last PHP function. And the reason I cannot pass it directly is because this variable depends on which button is clicked.