AJAX Load function from php - php

;-) I have a hit a brick wall. Today I have met Ajax for the first time in my life. And I understand, that my question can seem very stupid. But I need your help.
I have a php file (for ex. index.php), it has an include (function.php). In the file function.php I have a function (for example function Jokes(){....}).
I want to use AJAX to load this function in my page. But I don't know how to do this... :-(
I have found some easy solutions (I use Jquery load function), but it's not what I want, because I have to use a separate file.
$('#jokes').load('jokes.php');
But I need something like:
$('#jokes').load('function.php','jokes()');
Any help or ideas? It will be appreciated.
P.S: Sorry for my bad English...))

What you can do is make one AJAX file, e.g. ajaxfunctions.php where you divide the functions.
$('#jokes').load("ajax.php", { 'function': 'jokes' } );
In your PHP file ajax.php you can do this:
<?php
include 'function.php';
if (isset ($_POST['function']) && $_POST['function'] == 'jokes')
{
echo jokes ();
}
?>
Does this help you?

The browser (and thus Ajax) can only make HTTP requests to the server.
It cannot execute specific functions.
You would have to write server side code that would recognise a particular URL (e.g. to a script that only calls that function, or a script that decides which function to run based on the query string) and execute a particular function in response to a request to it.

No, it's impossible to do it this way. You must make a request to a url. You can't just invoke a specific function from a php file.
Build a url wrapper for the function you need.

Don't do that.
Ajax is used to send or retrieve data, but what you are trying to do is to load a Javascript function defined in a PHP file (isn't it?).
Instead, just define your Javascript function in a JS file and load it via a script (or, if it should be loaded dynamically, you may use an AMD loader like require.js)
If you want to load a PHP function in JS, then it's not possible. You may call that function and fetch its result instead, though.

It doesn't work like this.
$('#jokes').load('jokes.php');
does an http request to jokes.php. If you want to execute a specific function, you can use a parameter, like this:
$('#jokes').load('jokes.php?func=jokes');
From your php script, get 'func' and execute the proper function.
...
function jokes() {
return 'some output';
}
...
$func='';
if (!empty($_REQUEST['func'])) {
$func=$_REQUEST['func'];
}
if ($func=='jokes') {
echo jokes();
}

You don't need AJAX for this. You want to put the Jokes() function in a JavaScript file, maybe called Jokes.js, include it in index.php and then call it normally in a JavaScript block in index.php.

Related

run php function, display the response, and start another php function

Run php script, display the response, and start another php script and then display it
I have two functions in php I would like to display the response of the first function in order to make loading less long
Exemple Pseudo-code
Function hello1()
{
echo 'hello1';
[...]
}
Show response and run another function
Function hello2()
{
echo 'hello2'
[...]
}
all php methods will be executed before rendering the html page. May you think about Javascript event handling or an method "injections" with Ajax request to a code behind file which is php based.
Regards,
Tobo

javascript call function from php file! (using yii)

I'm trying to call a function in a .js file from the php page where I'm displaying it!
What I want to do is something like this:
.php file:
...
function testFunc(){
return "2";
}
...
now I want to call this function inside the .js file!
Thaks in advance for your help
What you have tried?
Assuming the testFunc function is enclosed in proper script tags,
your .js file can call it quite simply like this:
testFunc()
Ideally, you include the .js file after testFunc is declared.
PHP runs server side. JavaScript (assuming you are using it in a browser) runs client side. If you want to execute this PHP function from the client side and get the result without reloading the page, you have to do an ajax request, which may be beyond your skills.
Look into jQuery Ajax:
http://api.jquery.com/jQuery.ajax/

Ajax call does not execute PHP function

I have written a very basic PHP pagination class and I'm trying to load the content with jQuery Ajax $.post requests. My class has two functions, one displays the content and the other one the pagination. If I don't use jQuery, but only PHP, everything works fine. Once I try to use jQuery, my displayContent() function never gets called, but the createNav() one does its job just fine(I mean the page numbers are loaded).
Here is my js.
http://goo.gl/BUZH1
And here is my PHP:
if($action=='displayArticles') displayArticles();
function displayArticles()
{
$content=new createPages(5,10);
$content->displayContent();
$content->createNav();
}
And here is the PHP class I just created. It was too large to add it in here, but I saved the code in text format.
http://goo.gl/Lv9Wl
Try dumping $_POST['action'], then check the output in the $.post response. It's pretty obvious that your if statement is resolving to false. :)
You can also try dumping $action, then checking that in the response. Good luck!
You can not call PHP functions after the page has been loaded. PHP is server side script, so you need AJAX to load a separate page, where your displayArticles function will be defined.

How do I trigger a php function on onclick event?

For instance I have a php function
<?php
function SayHi($msg){
return $msg;
}
?>
The following part triggers the onclick event
Click
How can I merge the two such that when I click on Click, the php function is invoked..?
You can not. PHP is executed serverside, while javascript is executed clientside.
THe generic solution for this is to use asynchronous Javascript (ajax) to call on your webserver, which in turn can invoke PHP code to do various things.
jQuery is a very nice library to handle this, and much more.
There are many ways to do this, most are complicated.
One easier way is:
<?php
function myFunc() { die('yay'); }
if (isset($_REQUEST['myButton'])) myFunc();
?>
<form><button name="myButton">Click Me</button></form>
Whaaat? PHP is server-side you cannot invoke a php function from javascript. However you can do this with ajax.
AJAX
You'll want to use AJAX. You need to call a server-side resource from the JavaScript code, and jQuery has a very handy function for making the call to the server. But making the call from the JavaScript is only half the story, you'll also need something on the server listening for that call. It would essentially be another PHP script which acts as a page in and of itself, but would return data in the form of (most likely) JSON instead of HTML. It's not meant to be human-readable, but rather to be a sort of web service for your JavaScript code to use.
You can find a simple example here.
please get involved with the basics of PHP: http://tut.php-quake.net/en/

jquery call for a remote php function

I have a selectbox that needs to be updated dynamically. I am using jquery to perform that using .load($url), where $url is the location of the php file to call. I am wondering if there is a way to call a specific function within that php file instead of calling the entire php file.
You could send a GET parameter as part of the AJAX request e.g. blah.php?func=1
Then in your PHP file:
if ($_GET['func'] == 1) {
do_something();
}
else {
do_something_else();
}
Or you could use something like CodeIgniter that structures its URL's as controller/function/parameter
no, you can't. but you can pass GET parameters to the URL, and use that URL as a controller.
for example:
.load("/ajax_controller.php?action=whatever")
You can pass a parameter to PHP script and user switch in script to select which action needs to be done

Categories