简体   繁体   English

使用动态php内容加载ajax网站的好习惯

[英]Good practice for ajax site loading with dynamic php content

I'm a bit swamped here. 我有点淹没在这里。 I got a project with rather dynamic content, so it's written in php. 我有一个相当动态的内容的项目,所以它是用PHP编写的。 Depending on the user input, different files get loaded. 根据用户输入,加载不同的文件。 So far no problem, but I also want to use ajax (jquery) to load the content (if user has js on) and that's where I'm stuck a little. 到目前为止没有问题,但我也想使用ajax(jquery)加载内容(如果用户有js),那就是我被卡住的地方。

For my php site structure I have something like this: 对于我的PHP网站结构,我有这样的事情:

switch ($input) {
    case 'foo': require 'foo.php';
    break;

    case 'bar': require 'bar.php';
    break;
}

and in the actual content area some functions like 并且在实际内容区域中有一些功能

<div id="content"><php echo content_function(); ?></div>
<div id="sidebar"><php echo sidebar_function(); ?></div>

Those functions are defined both in bar.php and foo.php. 这些函数在bar.php和foo.php中定义。 That way I don't have to struggle with different switch/if-else statements in the middle of my html and the whole file-handling is done in the switch statement. 这样我就不必在我的html中间使用不同的switch / if-else语句,整个文件处理都在switch语句中完成。

When loading with jquery now I apparently can't re-instantiate the functions in content and sidebar without reloading the page which would make the ajax load useless. 现在加载jquery时我显然不能重新实例化内容和侧边栏中的函数而不重新加载会使ajax加载无用的页面。 So I added some extra code to foo.php and bar.php to be able to instantiate each function seperatly into the appropriate divs (after emptying them beforehand for sure). 所以我在foo.php和bar.php中添加了一些额外的代码,以便能够将每个函数分别实例化到相应的div中(事先清空它们之后)。 Eg 例如

if(isset($_POST) && $_POST['load_content_func']) { echo content_function(); }
if(isset($_POST) && $_POST['load_sidebar_func']) { echo sidebar_function(); }

Then I just take the output of each ajax request and load it into content and sidebar: 然后我只是获取每个ajax请求的输出并将其加载到内容和侧边栏中:

var foo_or_bar; //depending on user input either foo or bar
if (foo_or_bar == 'foo') {

    $.ajax({  
      type: "POST",  
      url: "foo.php",  
      data: load_content_func,  
      success: function(html) {  
        $('#content').html(html);

    $.ajax({  
      type: "POST",  
      url: "foo.php",  
      data: load_sidebar_func,  
      success: function(html) {  
        $('#content').html(html);
}

Note: All of the code is just an example and written out of my head, doesn't matter if somethings wrong as long as you get the idea what I want to do! 注意:所有的代码都只是一个例子而且是从我脑海中写出来的,只要你知道我想做什么,如果有些错误就没关系!

Somehow it doesn't feel right, but being the odd perfectionist, I'm never really happy, so it might be just me. 不知怎的,它感觉不对,但作为奇怪的完美主义者,我从来没有真正开心,所以它可能只是我。 Is there a good practice for doing something like this and how do you usually solve this ? 做这样的事情是否有良好的做法,你通常如何解决这个问题? All suggestions welcome! 欢迎所有建议!

Here's the thing, and I hope it's not aggravating due to it being a generalized response and posing more questions than answers: 这就是事情,我希望它不会因为它是一个普遍的反应并提出更多问题而不是答案而加重:

The way you propose is that there is the potential for new content to be loaded depending on user interaction, and when that new content is loaded, a new sidebar is also loaded. 您提出的方法是,根据用户交互,可能会加载新内容,并且在加载新内容时,还会加载新的侧边栏。 I have some thoughts on this, and a caveat which is: I'm not the world's most experienced PHP developer. 我对此有一些想法,并提出一个警告:我不是世界上最有经验的PHP开发人员。 That said, here are my initial thoughts (they do not all relate to one-another): 也就是说,这是我最初的想法(它们并非都与彼此有关):

A) The reasons people use Ajax vary: to minimize the data being retrieved, to avoid the visual "flash" from reloading a whole page, and I'm sure there are other reasons. A)人们使用Ajax的原因各不相同:最小化检索的数据,避免重新加载整个页面的视觉“闪存”,我确信还有其他原因。 To decide if Ajax is even the tool for the job, you need to decide "why" you are using it. 要确定Ajax是否是工作的工具,您需要决定“为什么”使用它。

B) If you ALWAYS retrieve a "foo" sidebar to go with "foo" content, why not just fetch them at once? B)如果您总是检索“foo”侧边栏以使用“foo”内容,为什么不立即获取它们呢? No matter what your PHP logic is on the server side, it can return with one request: 无论您的PHP逻辑在服务器端是什么,它都可以返回一个请求:

<div id="content">.. all my content...</div>
<div id="sidebar">.. my sidebar ...</div>

There are very few well-structured pages out there that will have any markup separating the content from the sidebar. 很少有结构良好的页面会有任何标记将内容与侧边栏分开。 Yours might be one, though; 不过,你的可能是一个人; I don't know that for sure. 我肯定不知道。

C) If you ALWAYS retrieve "foo" sidebar to go with "foo" content, and you CAN'T return them in ordered markup (as above suggestion) you can still just ask for foo.php once, and in the success function you can parse html and send it to your two separate areas. C)如果您总是检索“foo”侧边栏以使用“foo”内容,并且您无法按顺序标记返回它们(如上所述),您仍然可以只请求foo.php一次,并在成功函数中可以解析HTML并将其发送到您的两个独立区域。

D) If you tightly couple your "foo" sidebars and content like that, you will probably need to re-engineer it later. D)如果你把你的“foo”侧边栏和这样的内容紧密结合在一起,你可能需要稍后重新设计它。 What if you decide that both "foo" and "bar" content areas can actually share "baz" sidebar? 如果您认为“foo”和“bar”内容区域实际上可以共享“baz”侧边栏怎么办? Your proposed way will mean that you'll need to have logic checking for "baz" content to go with "baz" sidebar. 您提出的方法意味着您需要对“baz”内容进行逻辑检查,以便与“baz”侧边栏一起使用。

E) If you don't like my suggestion to just get both content and sidebar at once, I see nothing terribly wrong with doing two separate and distinct Ajax calls, though. E)如果你不喜欢我的建议只是同时获得内容和侧边栏,我发现做两个独立的,不同的Ajax调用没什么特别的错误。 Some people will come up with clever ways to do it with an iterator so that you only need to specify "foo.php" once (even though two calls are being made) and it will automagically grab both. 有些人会想出一个聪明的方法来使用迭代器,这样你只需要指定一次“foo.php”(即使正在进行两次调用),它也会自动抓取它们。 But meh. 但是,好吧。 Why make code esoteric and hard to read when two separate and distinct calls make it very clear what the intended purpose is. 当两个独立且不同的调用使得非常清楚预期目的是什么时,为什么使代码深奥且难以阅读。

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

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