Creating jQuery AJAX requests to a PHP function

So far when creating AJAX requests I have been posting to a separate PHP file. Is it possible to create a jQuery AJAX request that calls a PHP function rather than posts to a separate page?

If you could send me any examples or documentation would be great.

AJAX requests call a URL (make a HTTP request), not a file, in most cases the URL is translated by the server to point at a file (or a php script in your case), but everything that happens from the HTTP request to the response that is received is up to you (on your server).

There are many PHP frameworks that map URL's to specific php functions, AJAX is just an asynchronous way to access a URL and receive a response.

Said URL CAN trigger the server to call a specific function and send back a response. But it is up to you to structure your URL's and server side code as such.

How to Use AJAX in PHP and jQuery - Code Tuts, After setting up the listener, we initiate the request by calling the open method of the XMLHttpRequest object. The readyState property value will� How to Create a jQuery Ajax Post with PHP. In this short tutorial, we are going to share with you how to create a jQuery Ajax post request with PHP. Also, you can see how to post JSON data with jQuery easily and quickly. After checking out this tutorial, you will be able to create ajax post requests much more easily.

I believe there's a fundamental misunderstanding of how the technology works here.

AJAX (Javascript), Flash, or any client-sided technology cannot directly call PHP functions (or other languages running on the server). This is true for going the other way around as well (eg: PHP can't call JS functions).

Client and server codes reside on different machines, and they communicate through the HTTP protocol (or what have you). HTTP works roughly like this:

Client (eg: browser) sends a REQUEST -> Server processes request and sends a RESPONSE -> Client gets and displays and/or processes the response

You have to see these requests and responses as messages. Messages cannot call functions on a server-side language directly 1, but can furnish enough information for them to do so and get a meaningful message back from the server.

So you could have a handler that processes and dispatches these requests, like so:

// ajax_handler.php
switch ($_POST['action']) {
    case 'post_comment':
    case '....':
        output_error('invalid request');

Then just have your client post requests to this centralized handler with the correct parameters. Then the handler decides what functions to call on the server side, and finally it sends a response back to the client.

1 Technically there are remote procedure calls (RPCs), but these can get messy.

Simple Ajax request example with JQuery and PHP., Simple examples on how to carry out an Ajax request with JQuery and PHP. Note that this function has a parameter called data, which will contain the output� So far when creating AJAX requests I have been posting to a separate PHP file. Is it possible to create a jQuery AJAX request that calls a PHP function rather than posts to a separate page?

If you're asking whether you can call any arbitrary PHP function with AJAX the answer is no*, for obvious security reasons (in addition to the technical reasons). You could make a PHP script that does different things depending on what parameter it's given (for example, execute a single function) if you don't want to create multiple separate files.

*Although you could make a script that would execute any arbitrary PHP command coming from the client, but that would be very, very, very unwise.

jQuery Ajax GET and POST Requests, The following example uses the jQuery $.get() method to make an Ajax request to the "date-time.php" file using HTTP GET method. It simply retrieves the date� Simple Ajax request example with JQuery and PHP. Back when I was first starting out with JavaScript and JQuery, I was a bit frustrated by the lack of simple introductory JQuery Ajax examples. Most of the tutorials that I found back then were needlessly complicated or incredibly specific.

Short answer is "no" but the real answer is that you can fake it. NullUserException's answer is good. You create a server that will take the function name and its parameters. Then the server executes the function, and returns the value.

This was done a while back via a protocol called XML-RPC. There was also an effort called JSON-RPC that used some JS techniques.

One things that's cool about JS is that you can do things like this:

var base64_decode = create_remote_call('base64_decode');

function create_remote_call(name) {
  return function(x) { 
                   function(d){return d;}); 

A call to base64_decode('sarefdsfsaes') will make a ajax request and return the value.

That code probably won't work because it hasn't been tested, but it's a function that produces a function that will call the server, and then return the value. Handling more than one argument requires more work.

All that said... in my experience, it's usually good to make all network communications explicit instead of disguising it as a regular function.

jQuery Ajax Function: How to Make Asynchronous HTTP Requests, Learn how to use $.ajax(), the most powerful jQuery Ajax function, to perform They are convenient methods for making Ajax requests in a few lines of code. of my talk Modern front-end with the eyes of a PHP developer. In this tutorial we will create a PHP script that will search the flickr database for posted values using the flickr API. On the client site we will use some simple HTML search form and some easy to understand jQuery Ajax code to handle the requests. If you like to test this tutorial code your should try our flickr Ajax search demo. HTML search

you may achieve the same result using a bridge, like my phery library you can call PHP functions directly from Javascript and deal with the value. The AJAX is bound to DOM elements, so you can manipulate the calling DOM or just use jQuery from the PHP side. An example would be:

  'phpfunction' => function(){
     return PheryResponse::factory()->jquery('body')->addClass('whoops');

and in the javascript side (or HTML)


the equivalent to the from John Kawakami answer, using phery is:

function base64($data){
  return !empty($data['encode']) ? base64_encode($data['content']) : base64_decode($data['content']);

  'base64' => 'base64'
function base64(content, decode, output){
  phery.remote('base64', {'content': content, 'encode': decode ? 1 : 0}, {'type':'text'}).done(output);

base64('asdf', false, function(data){
  console.log(data); // or assign to some variable

since AJAX is asynchronous and you can't just return a value from the AJAX call, you need a callback, but this would suffice.

Submitting Forms Without Reloading The Page: AJAX , You will learn not only how to make an AJAX call, but also how to do it the best way using <?php /* Plugin Name: Report a bug Description: Allow your visitors to report a bug in And created a callback method with form HTML: Instead of doing it in plain JavaScript we are going to use jQuery which� Now we have jQuery code ready to make Ajax requests, we can create a new PHP file that will respond to Ajax requests made by the code above. This PHP file will work in background and have no direct contact with user.

How to use jQuery AJAX method to call a PHP page, jQuery AJAX method to call a PHP page. To download the source code visit this jQuery Duration: 18:04 Posted: 7 Oct 2017 The jQuery $.ajax() function is used to perform an asynchronous HTTP request. It was added to the library a long time ago, existing since version 1.0. It was added to the library a long time ago

A simple jQuery Ajax Request to PHP, This is an updated version on how to do simple Ajax requests using jQuery < script> jQuery(document).ready(function() { //you should replace� If it is, clear the content of the txtHint placeholder and exit the function. However, if the input field is not empty, do the following: Create an XMLHttpRequest object; Create the function to be executed when the server response is ready; Send the request off to a PHP file (gethint.php) on the server

How to handle AJAX request on the same page - PHP, In this tutorial, I show how you can handle AJAX request on the same page and <!doctype html> <html> <body > <form method='post' action> <input src="https ://"></� I'm attempting to use jQuery each in my new website code. At the moment, for each div class "added" on the page, it loops through them and submits an AJAX request to my PHP script which then updates the database. The problem with this is, if the user created 100 items, 100 requests would be sent which is just not acceptable.

  • No, unless you call a PHP script. As far as i know, you cant communicate with a PHP function on the same page
  • AJAX (JS) cannot call individual PHP functions. It doesn't work like that.
  • "I believe there's a fundamental misunderstanding of how the technology works here." I whole-heartedly agree. "Client and server codes reside on different machines"... I think this is the part that stumps a lot of people. HTML, Javascript, and PHP all reside side-by-side on the server, but Javascript executes on the client side. I don't think people understand that getting the Javascript and calling a PHP script from Javascript are two separate requests to the server. +1 for the illustration. :)
  • what do you mean by arbitrary?
  • @jondavidjohn Anything at all, unrestricted
  • I don't think he's wanting to let his user's run unrestricted php on his server, he's just wanting to organize his php into functions instead of running individual files/scripts.
  • I dunno, I read the question as "using AJAX can you execute PHP functions instead of running PHP scripts". I could be wrong, though.
  • The way I see it, security is not the issue here, it's purely technical. ie: you could have a client connected to the server (eg: SSH) directly and execute arbitrary commands, PHP or not.
  • "A call to base64_decode('sarefdsfsaes') will make a ajax request and return the value" - No, it won't return the value.