This question already has an answer here:
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?
</div>
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.
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()