简体   繁体   English

从js调用php(带有ajax)

[英]calling php from js (w/ ajax)

I'm building a basic forum where every post contains some text, first and last name, and the date the message was written. 我正在建立一个基本论坛,每个帖子都包含一些文本,名字和姓氏以及消息的撰写日期。 I'd like to have the board update with AJAX constantly, and to add new messages on the fly as they are written. 我想不断用AJAX更新开发板,并在编写新消息时即时添加新消息。 I have a file, getlatest.php?date=... that retrieves all messages from the date in the $_GET till NOW() . 我有一个文件getlatest.php?date=... ,它检索从$_GET的日期直到NOW()所有消息。 It returns the data as an array of objects. 它以对象数组形式返回数据。 Lastly, I JSON-encode the data. 最后,我对数据进行JSON编码。 I call this function from some AJAX code, like so: 我从一些AJAX代码中调用此函数,如下所示:


 setInterval("update()", 5000);
                function update(){
                    $.get("getlatest.php", {
                        date: "2009-06-23_16:22:12" //this is just a date I
                                                    //entered for testing
                    }, function(forumdata){
                                           //do something with forumdata here?
                    }, "json");
                }

Now I have the data within forumdata , as in forumdata[0].first_name ect. 现在,我已经将数据存储在forumdata内,如forumdata[0].first_name

I would like to now use PHP to display the comment, like so: 我现在想使用PHP来显示注释,如下所示:

$forumdata = json_decode(forumdata);
foreach ($forumdata as $value)
{
    $newpost = new Post($value); // Post being some class that gets the data from 
                                 // $value and converts it to HTML + CSS
    $newpost->displayPost();     // some function that echo's the HTML
}

I realize what I'm trying to accomplish here is slightly unfeasible, since PHP is a server-side language, and the calculations are at this point client-side, but is there some way (maybe through AJAX?) that I could use PHP to manage the data, once I retrieve it through javascript? 我意识到我要在这里完成的工作有点不可行,因为PHP是一种服务器端语言,并且此时的计算是在客户端进行的,但是有什么方法(也许是通过AJAX吗?)可以使用PHP我通过javascript检索数据后,如何管理数据?

My main reason for wanting to do this is my total lack of knowledge in javascript, so any alternatives for translating the forumdata variable into blocks of HTML and CSS are also great for me. 我想要这样做的主要原因是我对javascript完全缺乏知识,因此将forumdata变量转换为HTML和CSS块的任何替代方法对我来说也非常forumdata

The way to avoid the mismatching of dates/times between the server and client is to have each entry have a unique id. 避免服务器和客户端之间日期/时间不匹配的方法是使每个条目都有唯一的ID。 This way, you're able to give a specific record rather than a relative time for your offset. 这样,您就可以提供特定记录而不是相对时间来抵消。 Primary keys in MySQL are an example of this. MySQL中的主键就是一个例子。

You can have getlatest.php return HTML code instead of JSON data. 您可以让getlatest.php返回HTML代码而不是JSON数据。 That way, you just have to inject the returned HTML code into your document. 这样,您只需要将返回的HTML代码注入文档中即可。

You should change getlatest.php (or create a new file, getlatesthtml.php) so it returns the HTML output of displayPost(). 您应该更改getlatest.php(或创建一个新文件getlatesthtml.php),以便它返回displayPost()的HTML输出。 Then "//do something with forumdata here?" 然后“ //在这里使用forumdata做些什么?” just needs to set .innerHTML of some container somewhere. 只需在某个地方设置某个容器的.innerHTML。

Well since you don't want to use too much JavaScript (don't be scared with the popular Libraries and Frameworks it is acutally a pretty easy thing to learn and can save you a lot of the hassle you would have by managing everything on the server side) you can still output just the HTML without decoding it to JSON. 好吧,由于您不想使用太多的JavaScript(不要被流行的Libraries and Framework所吓倒,因此,这是一件非常容易学习的事情,并且可以通过管理所有内容来节省很多麻烦服务器端),您仍然可以只输出HTML,而无需将其解码为JSON。 So the client does this: 因此客户端这样做:

 setInterval("update()", 5000);
                function update(){
                    $.get("getlatest.php", {
                        date: "2009-06-23_16:22:12" //this is just a date I
                                                    //entered for testing
                    }, function(forumdata){
                                           //do something with forumdata here?
            // e.g.
            $("#forum_entries").prepend(forumdata);
                    }, "html");
                }

And the PHP script (getlatest.php) just does this: PHP脚本(getlatest.php)就是这样做的:

<?php
$forumdata = find_posts_after_date($_GET('date'));
foreach ($forumdata as $newpost)
{
   // Echos just the post specific HTML not a whole HTML page
    $newpost->displayPost();     // some function that echo's the HTML  
}
?>

As nerdling already suggested you should not use the Date but the latext Unique ID of the post the client is currently displaying. 正如书呆子所建议的那样,您不应使用“日期”,而应使用客户端当前显示的帖子的唯一文本ID。

I see you've already accepted an answer, but you might want to think about learning to parse the json. 我看到您已经接受了答案,但是您可能想考虑学习解析json。 There are so many reasons why you'd want to let the javascript handle the display of the data. 有很多原因导致您希望让javascript处理数据显示。 Think about all of the web services with api's, they return some sort xml or json. 考虑一下带有api的所有Web服务,它们返回某种xml或json。 Considering you where you are now with jquery, it's as basic as learning about 'each()'. 考虑到您现在在使用jquery的位置,它与学习'each()'一样基本。 I did some googling, this link looks like it will get you started. 我做了一些谷歌搜索, 这个链接看起来会让您入门。 Leave comments if you'd like more help. 如果您需要更多帮助,请发表评论。

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

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