Call PHP function and return value to Javascript

admin

Administrator
Staff member
I am working with
Code:
Wordpress
and the
Code:
Javascript
will insert text in to the editor on the edit post.

I have two files, one is the
Code:
js
and another one is the
Code:
PHP
file. I want to call the
Code:
PHP
function to return the database value to the
Code:
Javascript
.

What I am doing:

<blockquote>
I have [value - X] points. //The Javascript will insert this into the editor. [value - x] is the value which return from the PHP function
</blockquote>

Here is my
Code:
JavaScript
:

Code:
   onsubmit: function( e ) 
{
var str = '';                                                                    
if(e.data.friend_cb ) 
{                                                                            
    str += 'I have [value - X] points. &lt;br&gt;&lt;br/&gt;';
}

editor.insertContent(str);



jQuery.ajax({
    url: 'http://localhost:8080/wordpress/wp-content/plugins/databaseConnection.php',
    type: 'POST',
    data: {functionname: 'getX', condition_code: condition_code},
        error:function(data)
        {
            alert("failed");
            console.log(data);
        },
        success: function(data) 
        {
            alert("success");                                                                                
            console.log(data); // Inspect this in your console
        }
});

And here is the
Code:
PHP
function:

Code:
if( !isset($_POST['condition_code']) ) 
 {
      $error .= 'No function no_friend!';          
      $condition_code = $_POST['condition_code'];
 }

    $functionName  = $_POST['functionname'];
          // $functionName = 'add_bonus_point';
    switch($functionName) {
        case 'set_no_friend':  


             //Check did it pass the functionName
            if( !isset($_POST['functionname']))
                  $error .= 'No function name!'; 
            else
                 $errorBool = false;
            break;
        case 'try_insert':
            getX();
            break;
        }


 function getX()
 {
     $x = 0;
     //Connect to database, get X value.
     return $x;

 }

How can I get the value X?

Thx a lot.