1

Hi I am using ajax to do a post request. I think what I want to do is call a function within a php file but am a little confused if and how you do this, could anyone shed any light on this? This is what I have in my js file:

  function callAjaxAddition2() {
  arguments0 = jQuery('#code').val();
  $.ajax({
     type: "POST",
     url: file.php",
     data: {code: arguments0},
     success: function(data) {
       request( $posted )     
    }
  });
  return false;
}

'request' is a function within the php file.

Update I think I should be able to trigger what I need to using this: http://docs.woothemes.com/document/wc_api-the-woocommerce-api-callback/ if I put the url into the url field however that doesn't seem to work, how might I use a callback with ajax post?

David
  • 139
  • 3
  • 15
  • a variable that is passed in on the server side – David May 06 '14 at 10:13
  • @sudharsan I would let the code be the way it is, specially for syntax errors (though it's obvious). – AyB May 06 '14 at 10:13
  • Your PHP code will not be executed at the client end, it executes on the server and then the HTML/JS code will be rendered, you will have to keep your request function in another php file then make a ajax call to that file, – powercoder23 May 06 '14 at 10:15
  • Try to check this related topic: http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php?rq=1 – Gino Pane May 06 '14 at 10:16

2 Answers2

0

First fix this line with the missing opening quote file.php".

You cannot call a PHP function through AJAX but can trigger when it needs to be called, the following demonstrates the same:

In your PHP, your code would be:

if(isset($_POST['code'])){
   #'code' here is the identifier passed from AJAX
   request($_POST['code']);
}

Once your function has been called, does the necessary and returns the output to AJAX, you can use the data parameter you have set to see what output was sent back from PHP:

success: function(data) {
       alert(data); //contains the data returned from the 
                    //PHP file after the execution of the function    
    }
AyB
  • 11,609
  • 4
  • 32
  • 47
0

Calling on a php file via ajax call is like running the script that you pass in the url parameter.
You don't get access to the inner functions, all you can do is pass data and get response.

If you want the request() function to be called in the script, you will have to call it in the php script.

Jite
  • 5,761
  • 2
  • 23
  • 37