0

I am trying to call another php file's function using ajax. Here is the Ajax Call :

function getBaseURL() {

         var pathArray = location.href.split( '/' );
         var protocol = pathArray[0];
         var host = pathArray[2];
         var url = protocol + '//' + host;
         return url+"/PhpProject2";
     }

   function getPageSaverUrl() {
return getBaseURL()+"/manager/content_manager/page_saver.php/SaveTest";
}

function savePage() {
 alert("Saving Page");
 var url = getPageSaverUrl();
 $.ajax({
   type: "POST",
   url: url,
   data: "name=hello",
   success: function(response){

     alert(response);
   }
 });
}

But i am not getting any response from the script. Can we call php funciton from ajax url parameter?

sohel14_cse_ju
  • 2,481
  • 9
  • 34
  • 55
  • You can use ajax to hit a website endpoint, regardless of the back end language. Have you tried writing the url out to the console to make sure it's right? Have you tried making a basic form to post to that url with the input to see if that works? (ex.
    blah blah blah
    ). Or you could try the request with a browser rest client (cRest for chrome, RESTful for firefox) to make sure your endpoint is working correctly.
    – Taplar Apr 25 '15 at 12:06

2 Answers2

0

You cannot call php function from ajax url parameter.

You can use parameter with ajax call, see following code -

 function getPageSaverUrl() {
  return getBaseURL()+"/manager/content_manager/page_saver.php?param=SaveTest";
 }

then in file page_saver.php, you need to fetch parameter 'param', which will have value 'SaveTest', then execute this function.

Try this, let me know if you have any further query.

Prakash
  • 171
  • 7
0

first of all the ajax and normal http request are both the same according to backend . They hit the server and explore the url you called .

if you call - "http://website.com/mypages/page1" .

It reaches the port 80 on the server website.com

if any software application listens that port will receive the request. in case of php app . The apache server receives the request and explore the url "/mypages/page1" inside the Document root configured . And find the directory "page1" inside "mypages" directly if not any rewrite rules. If the index file you configured is index.php it searches for it , and run it . In that index file you get the data using php variable as per the request type. if it is post data "$_POST" or if query string "$_get" or some other php stuff . And you have to call the required function as per your need through php logics.

In the above ajax the data has to be

data:{name:"hello"}

or otherwise if you need to send the form data use

$(form).serialize()
Vignesh
  • 496
  • 1
  • 4
  • 13