简体   繁体   English

使用php和javascript帮助更改页面上的内容

[英]Help changing content on the page using both php and javascript

So I am trying to have a dynamic tabs kind of thing using both php and javascript. 因此,我尝试使用php和javascript来制作动态标签。 To make it much easier to understand, remember the two choices on facebook (Recent news and most recent) or something like that, when you click on one of them it just changes the content that you see in the middle without updating the page. 为了使其更容易理解,请记住在facebook上的两个选择(最近新闻和最新选择)或类似内容,当您单击其中的一个时,它只是更改了您在中间看到的内容,而没有更新页面。 i know how to delete the content in a div for example, but after deleting the content in the div (innerHTML = "") I want to populate it with the option chosen from the database. 例如,我知道如何删除div中的内容,但是在div中删除内容后(innerHTML =“”),我想使用从数据库中选择的选项填充它。

So let's say I have an option (all) and an option (only this) The default is all so when I run the page, I will just get all. 假设我有一个选项(全部)和一个选项(仅此选项)。默认值是全部,所以当我运行页面时,我将得到全部。 However, if I click on only this, it will clear the div "my header" and will replace the content with the latest content from the database (database) and display it. 但是,如果仅单击此按钮,它将清除div“我的标头”,并将内容替换为数据库(数据库)中的最新内容并显示它。

For that I need both php and javascript, is there a sample or an easy way to do this before i start digging around. 为此,我需要同时使用php和javascript,在开始研究之前,是否有示例或简单的方法可以做到这一点。

((Sorry if is not clear but the facebook example should be clear enough)) ((抱歉,如果不清楚,但是facebook示例应该足够清楚))

What you're talking about is ajax . 您在说的是ajax

I would suggest a javascript library to help leverage this, like jquery . 我建议像jQuery这样的JavaScript库来帮助利用它。

It can be as cool as 它可以像

  $.post('serverScript.php',
         function(data) {
              $('#idOfDivToUpdate').html(data); //shove script data into div
         },'html' ); 

tutorial . 教程

Whatyou are looking for a is AJAX/PHP approach. 您正在寻找的是AJAX / PHP方法。

  • Clicking on the tab 单击选项卡
  • The current content gets removed. 当前内容被删除。 This is possible because it has a unique "id" attribute in the HTML code 这是可能的,因为它在HTML代码中具有唯一的“ id”属性
  • The server is asked for the new content. 要求服务器提供新内容。 This is the AJAX request that will be triggered after/while/... the content is removed. 这是AJAX请求,将在内容删除之后/同时/ ...触发。
  • The server sends back the code. 服务器发回代码。 This can be HTML, JSON, XML or similar. 可以是HTML,JSON,XML或类似名称。
  • You script recieves the answer, may "do" something with it (like some parsing or similar) 您脚本收到了答案,可以用它“做”一些事情(例如某些解析或类似操作)
  • The content will be placed on the page (again, this is possible due to an unique "id" 内容将被放置在页面上(同样,由于唯一的“ id”,这是可能的

This is basically the way it is done. 这基本上是完成的方式。

Check out the different JavaScript frameworks. 查看不同的JavaScript框架。 They all come with nice AJAX support: 它们都具有很好的AJAX支持:

And of course, SO is also a nice place to look at: https://stackoverflow.com/questions/tagged/ajax+php 当然,SO也是一个不错的地方: https : //stackoverflow.com/questions/tagged/ajax+php

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

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