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

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

Related

PHP return partial script

I am creating a php (let's call it pageX.php) file that produces some html. The html has a button that when you press it, I would like to send an AJAX request.
To avoid having multiple files, I want to send the AJAX request to the same file. (pageX.php)
I don't want the AJAX response to return the entire php generated response that it would have with a normal GET request to pageX.php. So I am looking for a php method to simply stop executing and return what it currently has.
I understand I can do this with a big if statement, but I don't like wrapping the bottom part of my code with all the braces. So I am almost looking for the equivelent of a "break" statement for php that will simply return the current php generated html.
Is that possible?
Send your ajax request to pageX.php?ajax=y now you can do this
<?php
if(!empty($_GET['ajax'])) {
//return ajax data
exit;
}
?>
//your Normal html code here

php run before ajax is completed

I know PHP runs first but is there a way to get PHP to wait on an ajax request and then run its script? I have a php script here that I want to run but I NEED a variable from my JS file in order for it to run successfully. So was wondering if it's possible?
What I have is a normal request in my JS:
var myvar = data;
$.get('phpscript.php', {myvar: myvar} );
And in PHP:
$myphp = $_GET['myvar'];
But if i echo $myphp it returns "undefined", if I alert it however It displays the value; which means the php script is running before it even gets the request from ajax. Any way I could make the PHP wait?
Thanks.
Put the PHP that requires a variable in its own script and call it from the ajax call, once the ajax call gets a response update the DOM as needed.
PHP runs on server, then javascript runs on client to make the ajax call, then PHP runs on server returning data, then the javascript gets the data and does something with it.
$.get('phpscript.php', {myvar: myvar}, function(data) {
$('.result').html(data);
});
Inside the php file have something like:
$myphp = $_GET['myvar'];
echo $myphp;
The short answer is, no, you can't make PHP wait. PHP only runs on the server-side, by the time the AJAX request is sent, by definition, the page is already been sent to the client.
You'll probably have to do some refactoring. If the variable absolutely needs to be used for a PHP function, then you may need to move that logic into 'phpscript.php' or (less optimally) you may need to issue another AJAX request when you get the response from the first.
But my guess is that more commonly, you'll probably just have to figure out how to do what you want with javascript. If all you want is something equivalent to a PHP echo, you'll want to use Javascript (or JQuery) DOM manipulation for that.
EDIT: I forgot to mention, the other option is simply to do all the PHP stuff on the server-side before you send the page at all, instead of AJAX you'd want to do something in PHP like including your other php script and calling methods from it. But, everything you do on the server-side, the user is sitting there looking at a blank screen waiting for the page to load. So this isn't an option for anything that's not very quick.

AJAX Load function from 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.

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.

stopping javascript from getting executed

is it possible to prevent further execution of javascript?
I include some javascript scripts with php in a header (with echo ''), but there are coming some other scripts later in the page which i can not always control, so it could be that my before included (with ) mootools javascript get later overwritten by another included mootools (which then possible is an older version, or is not complete etc.)
so is there a way that I can stop the js at one point so that later js code will not be executed?
kind of like the die(); function in php, but without that it stops the page from being loaded.
doesnt really exist.
but you can put everything into a function and "return;" any given time to exit the function which would stop the execution of the rest of the code within the function.
super simple example in standard JS:
function init() {
if(something happens) {
return;
}
}
init();
in terms of stopping the browser from executing other scripts within the page - not possible.

Categories