简体   繁体   English

Javascript / PHP-使用PHP从服务器检索数据并使用Javascript生成/重新排列是个好主意吗?

[英]Javascript/PHP - Is it good idea to retrieve data from a server using PHP and generate/rearrange it using Javascript?

Premise 前提

While developing a website, I came across a problem that had to do with putting together a webpage using Javascript and PHP . 在开发网站时,我遇到了一个与使用JavascriptPHP组合网页有关的问题。 I decided to ask this question, because I didn't know what kind of terminology I should use to find an answer (if there is any, I doubt it). 我决定问这个问题,因为我不知道应该使用哪种术语来找到答案(如果有,我对此表示怀疑)。

Background - what I have 背景-我有什么

Little background may help you understand why I asked this question. 很少的背景知识可以帮助您理解为什么我问这个问题。

I'm working on a list of persons. 我正在整理人员名单。 Administrator will basically have a WYSIWYG (What You See Is What You Get) -editor, where he can type in information about each person. 管理员基本上将拥有一个所见即所得(所见即所得)编辑器,在这里他可以输入有关每个人的信息。 Note that all these persons with all their information are visible on the same webpage, you don't have a separate page for each person. 请注意,所有这些人及其所有信息都可以在同一网页上看到,每个人都没有单独的页面。

Each person is retrieved from a MySQL database in PHP and generated on the page. 每个人都是从PHPMySQL数据库中检索并在页面上生成的。 The amount of attributes (email, address, name, role, bank account, etc.) is somewhat significant. 属性的数量(电子邮件,地址,名称,角色,银行帐户等)有些重要。

By clicking a button Add a new person on the page one can add a new person to the person list ( this functionality is implemented in Javascript using JQuery ). 通过单击页面上的添加新人员按钮,可以将新人员添加到人员列表( 此功能使用JQuery在Javascript中实现 )。 Via AJAX one can save changes to persons without being redirected to a separate page. 通过AJAX,可以将更改保存到人员,而无需重定向到单独的页面。

Getting to the problem 解决问题

Now, beneath the hood (that is, in the source code) lies the actual problem. 现在,实际问题出在后台(即在源代码中)。 I mentioned how the person list will be generated using PHP, which is fine. 我提到了如何使用PHP生成人员列表,这很好。 However, because I want to use Javascript to add a person to the list, I have to repeat much of the same code, although with slight variation, as in PHP. 但是,由于我想使用Javascript将一个人添加到列表中,因此我不得不重复很多相同的代码,尽管与PHP中的代码略有不同。 I utilize append -method JQuery provides to add a person to the person list. 我利用append -method JQuery提供的将一个人添加到人列表。 It looks like this: 看起来像这样:

$('#vsrk_henkiloLaatikko_nappulaUusiHenkilo').click(function(){
    $('#vsrk_henkiloLaatikot').append(
        "<div class='vsrk_henkiloLaatikko'>"+
            "<div class='vsrk_henkiloLaatikko_kuva'>"+
                "<img src='media/img/tuntematon.png' alt='Ei kuvaa'/>"+
            "</div>"+
            "<div class='vsrk_henkiloLaatikko_tiedot'>"+
                "<h2 class='vsrk_kenttaEditoitava' contenteditable='true'>Etunimi Sukunimi</h2>"+
                "<h3 class='vsrk_kenttaEditoitava' contenteditable='true'>Henkilön rooli</h3>"+
                "<p class='vsrk_kenttaEditoitava' contenteditable='true'></p>"+
                "<div></div>"+
                "<p><b>Henkilötunnus:</b></p>"+
                "<p class='vsrk_kenttaEditoitava' contenteditable='true' style='min-height:28px'></p>"+
                "<p><b>Osoite:</b></p>"+
                "<p class='vsrk_kenttaEditoitava' contenteditable='true' style='min-height:28px'></p>"+
                "<p><b>Puhelinnumero:</b></p>"+
                "<p class='vsrk_kenttaEditoitava' contenteditable='true' style='min-height:28px'></p>"+
                "<p><b>Sähköposti:</b></p>"+
                "<p class='vsrk_kenttaEditoitava' contenteditable='true' style='min-height:28px'></p>"+
                "<p><b>Tilinumero:</b></p>"+
                "<p class='vsrk_kenttaEditoitava' contenteditable='true' style='min-height:28px'></p>"+
                "<p><b>Seurakunta:</b></p>"+
                "<select class='vsrk_henkiloLaatikko_valintaSeurakunta vsrk_yleinen_syoteNormaali' style='width:100%'>"+
                    <?php
                        foreach($GLOBALS["paikat"] as $paikka){
                            echo
"                               \"<option>" . $paikka . "</option>\"+\n";
                        }
                    ?>
                "</select>"+
                "<div>"+
                    "<p>Käyttäjätunnus:</p>"+
                    "<input class='vsrk_yleinen_syoteNormaali' type='text' placeholder='Ei käyttäjätunnusta' style='width:100%'/>"+
                    "<p>Näkyvyys:</p>"+
                    "<select class='vsrk_yleinen_syoteNormaali vsrk_henkiloLaatikko_valintaNakyvyys' style='width:100%'>"+
                    "<option selected='selected'>Näkyvissä</option>"+
                    "<option>Piilotettu</option>"+
                    "</select>"+
                "</div>"+
                "<div>"+
                    "<input class='vsrk_yleinen_nappulaNormaali vsrk_yleinen_nappulaPurppura vsrk_henkiloLaatikko_nappulaPoistaHenkilo' style='margin:2px 0 2px 0; width:100%' type='button' value='Poista tämä henkilö' />"+
                    "<input class='vsrk_yleinen_nappulaNormaali vsrk_yleinen_nappulaPurppura vsrk_henkiloLaatikko_nappulaTallennaHenkilo' style='margin:2px 0 2px 0; width:100%' type='button' value='Tallenna henkilötiedot'/>"+
                "</div>"+
            "</div>"+
        "</div>"
    );
});

The problem is that now I have to maintain both Javascript and PHP to contain the same information. 问题是现在我必须同时维护Javascript和PHP以包含相同的信息。 This kind of double work can be little irritating, especially if you have to use two different languages to do the job. 这种双重工作可能会有点恼人,特别是如果您必须使用两种不同的语言来完成这项工作时。

Thinking about a solution 考虑解决方案

Now, being somewhat lazy, but also "reasonable", I came to think of a solution: what if I could gather information from the database in PHP into some useful form, get that data using Javascript and then use Javascript on the client side to generate the actual person list using my append -method (or something to that effect). 现在,由于有些懒惰,但又“合理”,我想到了一个解决方案:如果我可以将PHP数据库中的信息收集成某种有用的形式,然后使用Javascript获取该数据,然后在客户端使用Javascript,该怎么办?使用我的附加方法(或类似的方法)生成实际的人员列表。 This would mean that once I edit this "append-method", I can not only generate the person list once the page is loading, but also add new persons to the list using the same method. 这意味着一旦我编辑了此“附加方法”,我不仅可以在页面加载后生成人员列表,还可以使用相同的方法将新人员添加到列表中。 No additional duplicates, and modifing types of information the persons contain would become easier. 没有其他重复项,并且修改人员所包含的信息类型将变得更加容易。

Now, the ultimate question... 现在,最终的问题...

...is this a good idea? ...这是一个好主意吗? Also, what kind of storage form would you recommend so that Javascript can "rearrange" the data retrieved using PHP, if this happens to be a good idea? 此外,如果碰巧是个好主意,那么您将推荐哪种存储形式,以便Javascript可以“重新排列”使用PHP检索的数据?

The short answer is yes. 简短的答案是肯定的。

The long answer is that what you're talking about is essentially separating your view from your model. 长答案是,您所谈论的实际上是将视图与模型分离。 When you do this you don't need to use the same programming language to retrieve and organize data and to present it. 执行此操作时,您无需使用相同的编程语言来检索和组织数据并呈现数据。

Typically speaking the "useful form" you are referring to in order to organize data and make it usable by Javascript is going to be either XML or JSon. 通常来讲,您要用来组织数据并使之可被Java使用的“有用形式”将是XML或JSon。 Because JSon is based on Javascript, I tend to use that. 因为JSon基于Javascript,所以我倾向于使用它。

Beyond JQuery there are several Javascript frameworks which are in use such as AngularJS and Ember. 除了JQuery,还有几个Java框架在使用中,例如AngularJS和Ember。 These serve the same purposes but are designed specifically to be a front-end fluid programming language. 它们具有相同的目的,但专门设计为前端流体编程语言。 Larger companies tend to gravitate to these languages and they tend to scale well. 较大的公司倾向于使用这些语言,并且它们的规模往往很好。

Regardless of what you use to present your data, the key is that it must work for your project. 无论您使用什么方式呈现数据,关键是它都必须对您的项目有效。 JQuery or even vanilla Javascript can do simple jobs such as parsing JSon and when you separate your project layers you will be better able to take advantage of the right tool for the job rather than sticking with PHP for the whole time. JQuery甚至是普通的Javascript都可以完成简单的工作,例如解析JSon,并且当您分离项目层时,您将能够更好地利用合适的工具来完成工作,而不是始终坚持使用PHP。

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

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