开发者

Dynamically create HTML via PHP or jQuery ajax? Which is faster or better method?

Hi I just want to know what the best practice is for dynamically creating html. I can do it in two ways

Direct PHP

<div id='id-here'>
    <?php
        $user->id = $_GET['id'];
        $user->displayUserInformation( );
    ?>
</div>

jQuery ajax(js script called on page load)

$.ajax({
    type: 'GET',
    url: 'inc/user_information.php',
    data: 'user_id=user_id', //assuming user_id value was already set.
    success: function(html)
    {
        $('#user_information').empty().html(html);
    }  
});

Note: This code doesn't exist and is purely for sho开发者_Python百科wing what I mean^^ I also know jQuery load, but prefer to use jQuery ajax for complex stuff.

Thanks!


The PHP method is certainly more reliable, as it doesn't require javascript in the client. For information that isn't expected during a page's lifetime, or indeed a user's session it also makes a lot more sense. I don't imagine the information on a user is likely to change that much during a page view.

However, if there is some data that's expected to change, say a post count or something, then use PHP to set the initial value, then use ajax to update it only when the value actually changes.


I wouldn't worry about which is faster... the difference would probably be negligible. But bear in mind that some users do have JavaScript turned off... if you want to support those users, it's worthwhile taking the extra effort to do it in PHP.

My rule is that if it can be done on the server, do it there. Then you can be absolutely sure of the results for all users.


u get it all wrong, first one is not a 'dynamically created html", user sent a request, PHP process it, and return the HTML, and your browser render it

2nd one is your browser already load HTML, and u try to use jquery to simulate another request of the same process of the 1st one


In your examples, if you show user info like this, method 1 will not require another data fetching from the server as in example 2 (2 HTTP requests total, 1 for original webpage, 1 for the ajax), so it is faster.

usually, generating static data inside a page like this (in example 1) is different from AJAX, where content is provided to the user, and only update with the new data using AJAX without updating the whole page's content.

Maybe what you mean is: should the data be provided together with the original webpage, or should it be left empty, and then use AJAX to fetch the data to display it. It would be better usually to provided data at first, instead of letting the user wait for another trip to the server to see the data.


I believe that loading from PHP as static loading would be better and more reliable. However loading from AJAX will push the results one time, not as static loading, data will be loaded in portions...


Along the same lines, which is faster within a php file. This is more for SEO and "best practice" than for actual user experience. I'm using wordpress, and I'm working within php files. Most of it is php, but I have four lines of html within the file. All are exactly the same. I could loop through the four lines with php, or copy and paste the four lines of html. I don't expect to ever change the code, so php doesn't seem to present any other benefits.

Here's my code: HTML version (well mostly)

    <img src="<?php echo get_bloginfo('template_directory').'/images/bracket.png';?>" class="bracket" />
    <img src="<?php echo get_bloginfo('template_directory').'/images/bracket.png';?>" class="bracket" />
    <img src="<?php echo get_bloginfo('template_directory').'/images/bracket.png';?>" class="bracket" />
    <img src="<?php echo get_bloginfo('template_directory').'/images/bracket.png';?>" class="bracket" />

OR the php:

 for($i=0;$i++;$i<4){ ?>    //start loop, 4x
   <img src="<?php echo get_bloginfo('template_directory').'/images/bracket.png';?>" />
   //Image path using php
 <?php }   ?>        // end loop

THANKS ALL!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜