简体   繁体   English

发送一个PHP + AJAX的帖子

[英]Send a post with PHP + AJAX

Context 上下文

I'm working on a project that I'd like to make more dynamic with PHP + AJAX. 我正在开发一个项目,希望通过PHP + AJAX使其更具动态性。 I'm using jQuery for the AJAX part because I totally suck in Javascript, and anyway it seems to be worth something. 我正在使用jQuery作为AJAX部分,因为我完全厌倦了Javascript,无论如何它似乎是值得的。 I reached a point where my application needs to use a form to send a post, and I want to use an ajax call to perform this action. 我的应用程序需要使用表单来发送帖子,我想使用ajax调用来执行此操作。 In the page I'd like to send the post, there is also a list of the most recent 15 posts submitted. 在我要发送帖子的页面中,还有一份最近提交的15篇帖子的列表。

First question: Should I just forget about it and use just PHP? 第一个问题:我应该忘记它并仅使用PHP吗?

The action 那个行动

The user writes something in the <textarea></textarea> and clicks on a <a id="myPostSubmit">Submit</a> that is the handler that I'll manage on the jQuery script with something like $("#myPostSubmit").live('click', function() { /* here i make the ajax call */ }); 用户在<textarea></textarea>写入内容,然后单击<a id="myPostSubmit">Submit</a> ,这是我将在jQuery脚本上管理的处理程序,类似于$("#myPostSubmit").live('click', function() { /* here i make the ajax call */ }); . If the post is successfully submitted we are going to do something (I'll talk about it in the next section), either we will alert the user using my showAlert(text) function, that shows a black box for 4 seconds with the text in it. 如果帖子成功提交,我们将会做一些事情(我将在下一节中讨论它),要么我们将使用我的showAlert(text)功能提醒用户,该功能显示带有text的黑盒4秒在里面。

Second question : Should I manage the click event in any other ways? 第二个问题 :我应该以任何其他方式管理点击事件吗? Should I create a function, such as sendpost(post) and attach it into the HTML onclick="" event? 我应该创建一个函数,例如sendpost(post)并将其附加到HTML onclick=""事件中吗?

If the post is successfully sent 如果帖子成功发送

I'd open a discussion about 2 options: 我将讨论两个选项:

  1. We refresh the page [not actually loading the entire page but making another ajax call that retrieves the posts lists and makes disappear the old one, load the PHP file to retrieve the new posts (including the one we just sent), and then make the post list appear]. 我们刷新页面[实际上并没有加载整个页面,而是进行另一个ajax调用,检索帖子列表并使其消失旧的,加载PHP文件以检索新帖子(包括我们刚刚发送的帖子),然后制作帖子列表出现]。 Pro : 1) We are sure that what the user is reading after the post list is loaded is the real post sent. 专业版 :1)我们确定在帖子列表加载后用户正在阅读的内容就是发送的真实帖子。 So it actually double checks the action. 所以它实际上是双重检查动作。 2) We load also some possible posts sent in the mean while. 2)我们还加载了一些可能发布的帖子。 Cons : 1) We have to create a PHP file that gets the post list template, slicing the template of that page in 2 files. 缺点 :1)我们必须创建一个获取帖子列表模板的PHP文件,将该页面的模板分成2个文件。 2) It doesn't really seems that smooth to me. 2)对我来说似乎并不那么顺利。

  2. We just use Javascript to get the post template, add it to the list. 我们只需使用Javascript获取帖子模板,将其添加到列表中。 Pro : 1) We make it really smooth, without reloading the entire page. Pro :1)我们使它非常平滑,而无需重新加载整个页面。 2) We don't need of any PHP file to reload the page. 2)我们不需要任何PHP文件来重新加载页面。 We just use Javascript (jQuery). 我们只使用Javascript(jQuery)。 Cons : 1) How do we get the post html template to add it to the list? 缺点 :1)我们如何获得post html模板将其添加到列表中? 2) How do we get the user (logged) informations without PHP? 2)如何在没有PHP的情况下获取用户(已记录)信息?

Third question : Is it better the 1st or the 2nd solution? 第三个问题 :第一个或第二个解决方案是否更好? Would you provide a even better 3rd solution? 您会提供更好的第三种解决方案吗?

The PHP page PHP页面

The PHP page that will receive this AJAX call is : ?p=action&a=sendpost . 将接收此AJAX调用的PHP页面是: ?p=action&a=sendpost The page require $_POST['myMessage'] to be set and not empty and nothing else. 该页面需要设置$_POST['myMessage']而不是空的,没有别的。 The page itself will get all the user infos from the cookies and will manage to perform the needed query. 页面本身将从cookie中获取所有用户信息,并将设法执行所需的查询。

The application 应用程序

It is divided in 3 parts: App , Template , Library . 它分为3个部分: AppTemplateLibrary Basically each page of the application has its own .app.php and .tpl.php file. 基本上,应用程序的每个页面都有其自己的.app.php.tpl.php文件。

  1. The .app.php file manages the building of the basis of the page, using classes and other stuff from the library. .app.php文件使用库中的类和其他内容来管理页面基础的构建。 In our case it retrieves datas from the database and put them into variable. 在我们的例子中,它从数据库中检索数据并将其放入变量中。
  2. The Template is called at the end of the .app.php file. 该模板在.app.php文件的末尾被调用。 The .app.php file send to the template the retrieved data and the .tpl.php file outputs them. .app.php文件将检索到的数据发送到模板, .tpl.php文件将它们输出。
  3. The library is used to contain the classes and functions we need in the application file. 该库用于包含应用程序文件中所需的类和函数。

Fourth question: Is this a good way of manage my web application? 第四个问题:这是管理我的Web应用程序的好方法吗?

Edit: What about returning the alert message to the user? 编辑:如何将警报消息返回给用户?

I read about an option, inside $.ajax() that will manage the response on success or in error. 我在$.ajax()中阅读了一个选项,该选项将管理成功或错误时的响应。 The documentation about it is very simple and I didn't get it. 关于它的文档非常简单,我没有得到它。

Fifth question: How should I return (from the PHP file) the error or the success? 第五个问题:我应该如何(从PHP文件中)返回错误或成功?

You seem to be on the right track with everything. 你似乎与一切都在正确的轨道上。 There are lot of opinions called "best practices" about how to exactly attach event handlers, how to reload the data on the page and how to organize your application, etc, but I personally would rather build more software instead of worrying about details like that. 关于如何准确地附加事件处理程序,如何在页面上重新加载数据以及如何组织应用程序等,有很多关于“最佳实践”的意见,但我个人宁愿构建更多软件而不是担心像这样的细节。 The details will come to you eventually. 详细信息最终会告诉您。

I personally find that updating whole chunks of server-side-rendered HTML on the page is more robust solution, but I have seen people getting excellent results with templates. 我个人发现,更新页面上的服务器端渲染HTML的整个块是更可靠的解决方案,但是我看到人们使用模板获得了很好的效果。

First question: Should i just forget about it and use just PHP? 第一个问题:我应该忘记它并仅使用PHP吗?

Well, you application will relay on JavaScript if you use ajax, this days i think it just fine ;) 好吧,如果你使用ajax,你的应用程序将继续使用JavaScript,这几天我认为它很好;)

Second question: Should i manage the click event in any other ways? 第二个问题:我应该以任何其他方式管理点击事件吗? Should i create a function, such as sendpost(post) and attach it into the HTML onclick="" event? 我应该创建一个函数,例如sendpost(post)并将其附加到HTML onclick =“”事件中吗?

Create a function and bind onclick. 创建一个函数并绑定onclick。 Code will be more readable ;) 代码将更具可读性;)

Third question: Is it better the 1st or the 2nd solution? 第三个问题:第一种或第二种解决方案更好吗? Would you provide a even better 3rd solution? 您会提供更好的第三种解决方案吗?

My solution: ajax submit the form and on callback insert new comment in to the list or display error message if user can't comment. 我的解决方案:ajax提交表单并在回调时将新注释插入列表或显示错误消息,如果用户无法发表评论。 Check jQuery serilize() for submitting forms data with ajax. 检查jQuery serilize()以使用ajax提交表单数据。

Fourth question: Is this a good way of manage my web application? 第四个问题:这是管理我的Web应用程序的好方法吗?

It's just fine ;) When you application get's bigger you will have to redesign it, but don't do it know, do it when current solution becomes to hard to work with. 很好;)当您的应用程序变大时,您将不得不重新设计它,但不知道它是什么,当当前的解决方案变得难以使用时就去做。 Read some good book on building MVC framework. 阅读一些关于构建MVC框架的好书。 And on programming patterns in general. 以及一般的编程模式。

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

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