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.
Related
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
;-) 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.
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/
Ok so trying to get a page together that needs to connect to our SQL database. So on the javascript page I have functions that will autocomplete a textbox with data out of our mysql DB and then I need to send it to other functions and classes so that it will then look in our SQL DB and return some data. The problem I have is trying to get the .GET call to call in the php page, with the function that calls the class in which I need to get into for the SQL call. I have it setup somewhat but trying to figure out how to send the data through with it as well as just get clarification on how to work the .GET function.
Javascript page:
$.get("dsl_validate.php", calldsl(job));
Php Page
function calldsl($job){
var $dsljob = $job
hasfunctioncode($dsljob);
}
The hasfunctioncode function is in my DSL class page that will return the info I need. Any help on if I am in the right direction or not?
It looks like you're trying to physically call the PHP function calldsl() from the JavaScript. This... isn't right. (I'm assuming the $.get() you're using is from jQuery, please correct me if that assumption is incorrect.)
What $.get() does is simply call a resource on the web server. It doesn't have any knowledge of the server-side code (nor should it, for a number of reasons). From the perspective of the server-side code, there's no difference between a page being called via $.get() vs. one that's just loaded in a web browser.
What you essentially need to do is create a PHP page which accepts arguments either as a form post or query string (if you're using $.get() then the query string is the way to go), does its server-side logic, and then simply outputs the results to the "page." In the case of calling the page via AJAX as you are here, it's a good idea to render the page content using JSON notation. (Don't forget to set the content-type header to "application/json" as well.)
Then what you're getting on the client-side from the $.get() call is the response body, which would be that JSON data. It's really just a "page" like any other, the only difference is the content-type telling the browser that it's JSON data and that it doesn't have HTML, just JavaScript objects. The success callback on the $.get() call (the function you pass it, or create in-line) would receive that response data as an argument and can do what you need to with it.
The way I understand jQuery.get(), the second argument is the "callback" (http://api.jquery.com/jQuery.get/). The callback will hand the results from your server therefore should be a function. Currently your code actually executes the function "calldsl" where you should be only passing a reference like so...
Javascript:
$.get("dsl_validate.php", function(response){
alert("yay I haz ajax! "+response)
});
PHP: "dsl_validate.php"
echo "this is some data from the server";
No, your are not in the right direction. The first parameter of the get method have to br the complete URL of the page, not just the script (this works if the script resides on the same directory of the javascript file, though). The .php file shall return somehting "usable" for you javascript (JSON, or HTML, or text, or... whatever). The "calldsl" function will be called AFTER the data has been returned from the call. Something like that:
$.get('dsl_validate.php?value=somevalue', function(data) {
alert("Data returned from dsl_Validate: " + data)
});
i think you are better off passing the function as a param to your php page
$.get("dsl_validate.php?calldsl="+job, function(data) {
$response = $(data);// create a jquery object from the response
});
`
and in your php file
create a switch statement that call the function based on the parameter
Mmm I think you are wrong, the second argument on your get function is the javascript function that will process de data returned by "dsl_validate.php". I mean, if that page returns "foo", job will contain "foo".
But in my experience is better to use the autocomplete plugin from Jquery UI
jquery autocomplete plugin
I want to use jQuery to include some PHP at a certain point in the page. When jQuery finds the class #site-index .sitetopic I want to append the content from a PHP file called images.php.
I presume I can use include or file_get_contents
Something like:
OnLoad.find('#site-index .sitetopic')
InsertPHP
You can't insert server side code on the client.
By the time Jquery would execute, the server is already done processing the page.
You could however use an iframe that you use jquery to add, or even use jquery's .load function http://api.jquery.com/load/
You cannot load and execute PHP code on the client side, but you can load the output of images.php.
I'm not sure if you want to place the output where #site-index .sitetopic is, or somewhere else:
// loads the output of images.php into the elements with class sitetopic
$('#site-index .sitetopic').load('images.php');
You can't insert server side code on client side - however you could call the script images.php which would do what you want to achieve.
Use jQuery's get(or post) function:
http://api.jquery.com/jQuery.get/
This allows you to send some variables to your server side script wich in turn returns some html or a variable you can use in your javascript.
As said before, you just can't.
And I assume it was for the sake of speed but don't worry, PHP doesn't include files with an HTTP request but it fetches them from the HDD, so your page won't load slower.