0
function my_action_javascript($val1, $val2) { 
  ?>
    <script type="text/javascript" >
    jQuery(document).ready(function($) {
        var data = {
            'email': '<?php echo $val1?>',
            'password': '<?php echo $val2?>'
    };

    jQuery.post({
      url: 'dummyurl',
      method: "POST",
      data: data,
      success: function (data) {
        console.log(data);
      }
    })
  });
    </script>
<?php
}

I got this function in my Wordpress Plugin. I parse in some data in the function and then i do a ajax request in Javascript. That all works just fine and i get the data array as response.

The Question is, how do i get the data from the Array in Javascript into my Variable in PHP so i can put the Data into my Wordpress Options?

6
  • can you post your array of response? Commented Sep 14, 2018 at 12:29
  • you can get $email = $_POST['email']; Commented Sep 14, 2018 at 12:32
  • {somedata: "datahere", moredata: "data", moredata: 66, data: null, bool: true} Commented Sep 14, 2018 at 12:33
  • ajax url is admin ajax? Commented Sep 14, 2018 at 12:34
  • sry i dont meant the $_POST, i mean the data i get from the success function of the AJAX Call Commented Sep 14, 2018 at 12:34

2 Answers 2

0

Try this.

you need to parse the response.

function my_action_javascript($val1, $val2) { 
  ?>
    <script type="text/javascript" >
    jQuery(document).ready(function($) {
    var data = {
        'email': '<?php echo $val1?>',
        'password': '<?php echo $val2?>'
    };

    jQuery.post({
      url: 'dummyurl',
      method: "POST",
      data: data,
      success: function (data) {
    console.log(data);
    var obj = jQuery.parseJSON( data);

    console.log(obj.somedata);
      }
    })
  });
    </script>
<?php
}
Sign up to request clarification or add additional context in comments.

3 Comments

I dont know if this is the solution, my question is how can i use my response in one other php function?
you cannot assign javascript variable to php variable
you can use curl post instead of ajax
0

You need to have a PHP script that gets called by the AJAX request that will write the data into your options table. There's a specific way of doing this with Wordpress: https://codex.wordpress.org/AJAX_in_Plugins

In a nutshell, you need to add an action parameter to your POST data and then hook your PHP callback function to this parameter. In your callback, you can then use update_option().

Comments

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.