简体   繁体   中英

dynamic fill out page with ajax, php js and html?

Suppose you have something like this HTML FILE:

<head></head>
<body>
  <div id="my_personal_div">
  </div>
</body>

Javascript FILE:

    $.(document).ready(){
    $.ajax({
       url:/any_php.php,
       ...
       ...
       if (msg){
         $("#my_personal_div").html(msg);
       }
    });

My any_php.php File:

echo "<img src=any_image.jpg />"

So the Idea is to have a Dynamic div into a html fill it out with some php content created by me, so I can feed multiple htmls with that div at once. What would be the problem of all this???? any cons??? any Huge security problem?, any better way to have a complete personal feeding, I mean, because I can puy there image, inputs, text, images, etc whatever I want from the php file.

The Cons is that it will make one more http request each time, and that takes time... And resources.

So it's a good way if you just have to change something on the page without reloading all the page.

A good way of practicing this also is when the user want to change page instead of reloading the all page just loading the content that had changed and using the push state on the browser to indicate that it's a new page. For example the website http://gizmodo.com/ use this.

No problem on the side of security by the way, it's just like loading a new page.

You have to make sure that the requests (GET and POST) are filtered by the php file you're accessing. If you don't do that anyone could run code that can harm your site (like SQL-Injections) just by modifying the request. Same goes for any other php script, so it's nothing really specific to ajax.

Anyways what do you want to create? A div that randomly shows some content each visit? Will it change after some seconds and show other content even if the user hasn't reloaded the page?

If you just want to randomly show some content you could do it without having to use ajax. Same goes if you just want to show some text inside a div.

Using ajax causes more connections which can end in a lot more stress for your server, depending on how much you use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM