Hello.

I'm not sure if anyone will be able to help me with this, because I've been unable to find a straight answer on the internet, and I thought it would be something that loads of people would want to do.

Let me make it clear I DO NOT WANT TO USE JQUERY OR ANY OTHER LIBRARIES FOR THIS. :)
Just a standard Ajax request I can dissect and learn from.

What I want to do is call a PHP function from the client using Ajax.

My example.php file:

<?php
function hello($arg){
    return 'Hello '.$arg; 
}
?>

HTML:

<input type="text" onkeyup="javascriptFunction(this.value)" />

And I need to be able to get and manage the response from the PHP hello() function in the javascriptFunction(). I know it is possible since the sAjax project does it, as do several other libraries/classes. These libraries have their limitations and I'd rather a barebones response so I can understand and tailor-suit it to my needs. However, I have no clue what to be sending in the request and whether it be GET or POST or Asynchronous or Synchronous etc etc.

I have some important necessities:

  1. Javascript libraries are a no-no. No JQUERY, Prototype, Mootools or anything else.
  2. The function cannot be called in the PHP file at any point.
  3. The PHP function must use return, not echo.

I look forward to hearing your ideas.
Thank you :)

Recommended Answers

All 3 Replies

I don't think you can do it directly. You would have to use some PHP MVC framework.
Natively PHP is executed by a file request, not an method request.

Thank you for your reply.

I had successfully written a pretty considerable essay outlining my thought process etc etc and subsequently discovered how to do it using a PHP object to generate JS. My head is still aching a bit, but thank you.

You are welcome.

The key feature for your issue is not javascript, but how to execute an PHP function directly from a URL.

You need something like this: url/file/function?params

If you are able to do it, then it's just a matter of posting to the URl via AJAX.

I don't know if it's possible cause i don't have much experience with PHP. But i know that asp.net has an structure called ServiceModel that allow you to do this, so i guess it might be one for PHP too.

In asp.net ServiceModel you can do something like this:

function executeServerFunction()
{
    MyNameSpace.MyClass.MyFunction(params..., sucessCallBackFunction, errorCallBackFunction);
}

Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.