0

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.

1 Answer 1

3

Change searchEmail to use the parameter:

function searchEmail(email) {
  var admin_url = admin_ajax.ajaxurl;
  $.ajax({
    type: "POST",
    url: admin_url,
    datatype: "html",
    data: {'action': 'search_notify_email', email: email},
    success: function() {
      searchNotification();
    },error:function() {
      searchNotificationError();
    }
  });
}

Then change the controller to use that parameter.

function search_notify_email(){
    $to = $_POST['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();
}

There's probably a more WordPressy way to get the parameter, but I don't know WordPress.

Sign up to request clarification or add additional context in comments.

13 Comments

But how is the PHP variable $author_email transfered to the javascript? Is this way of doing it correct? onclick="searchEmail('<?php echo $author_email?>');"
yes, that's fine. That's why I didn't put it into the answer.
But what I don't understand is how is this translated in to email in the javascript function?
Maybe you need to read a tutorial on how functions work. This is basically the same in all programming languages.
Did you change the data: option in the AJAX call as I showed?
|

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.