简体   繁体   English

动态填写页面与Ajax,PHP JS和HTML?

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

Suppose you have something like this HTML FILE: 假设您有类似以下HTML文件的内容:

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

Javascript FILE: JavaScript文件:

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

My any_php.php File: 我的any_php.php文件:

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. 因此,我的想法是将动态div插入html中,并用我创建的一些php内容填充它,以便我可以一次用该div填充多个html。 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. 任何巨大的安全性问题吗?有什么更好的方法来进行完整的个人喂食,我的意思是,因为我可以从php文件中随意获取图像,输入,文本,图像等。

The Cons is that it will make one more http request each time, and that takes time... And resources. 缺点是每次都会再发出一个 http请求,这需要时间和资源。

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. 例如,网站http://gizmodo.com/使用此网站。

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. 您必须确保所访问的php文件过滤了请求(GET和POST)。 If you don't do that anyone could run code that can harm your site (like SQL-Injections) just by modifying the request. 如果您不这样做,那么任何人都可以通过修改请求来运行可能危害您的网站的代码(例如SQL-Injections)。 Same goes for any other php script, so it's nothing really specific to ajax. 其他任何php脚本也是如此,因此ajax并没有真正特定的东西。

Anyways what do you want to create? 无论如何,您要创建什么? A div that randomly shows some content each visit? 每次访问都会随机显示一些内容的div吗? 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. 如果您只想随机显示一些内容,则可以使用ajax来完成。 Same goes if you just want to show some text inside a div. 如果您只想在div中显示一些文本,也是如此。

Using ajax causes more connections which can end in a lot more stress for your server, depending on how much you use it. 使用ajax会导致更多的连接,最终可能导致服务器承受更大的压力,具体取决于您使用服务器的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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