Use Jquery to Insert PHP content
I want to use jQuery to include some PHP at a certain point in the page. When jQuery finds the c开发者_运维技巧lass #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.
精彩评论