简体   繁体   English

我可以在使用 jquery ajax 的表单提交上同时调用同一 php 页面上的方法,也可以同时调用另一个 php 代码中的方法吗?

[英]Can i call a method on the same php page also a method in another php code simultaneously on form submit using jquery ajax?

help me!帮我! I am a php rookie and i am confused with the php request.我是一个 php 菜鸟,我对 php 请求感到困惑。

Working on MVC structure for view, I created a HTML form, and on form submit i want to do call two different methods sendMail() and validateData() available in two different controllers (php file) - say one in process.php and another in register.php .处理 MVC 结构的视图,我创建了一个 HTML 表单,在表单提交时,我想调用两种不同的方法sendMail()validateData()在两个不同的控制器(php 文件)中可用 - 比如一个在process.php和另一个在register.php

On form submit first I want to validate the data and save data to database using validatedata() .首先在表单提交时,我想验证数据并使用validatedata()将数据保存到数据库。 Then I want to send a verification mail to the user using sendMail() .然后我想使用sendMail()向用户发送验证邮件。

To handle the request (click event) I am using jquery ajax.为了处理请求(点击事件),我使用了 jquery ajax。 Ajax takes a single URL for posting data to the specified URL. Ajax 使用单个 URL 将数据发布到指定的 URL。 So how can I make two different URL requests in a single Ajax call, or say how can I call both the methods in a single Ajax call?那么如何在一次 Ajax 调用中发出两个不同的 URL 请求,或者说如何在一次 Ajax 调用中同时调用这两种方法呢? Please suggest me what i should do to solve this issue?请建议我应该怎么做才能解决这个问题? thanks in advance.提前致谢。

A controller should handle an entire request.控制器应该处理整个请求。

So if the user makes (for example) a request asking to create a new user account then a single controller should:因此,如果用户发出(例如)请求创建新用户帐户,则单个控制器应该:

  1. Validate the data the user has sent to make sure it is what is required before creating an account在创建帐户之前验证用户发送的数据以确保它是必需的
  2. Create the account in the database在数据库中创建帐户
  3. Send the email发送电子邮件
  4. Return a response saying if it worked or if there was an error返回一个响应,说明它是否有效或是否有错误

So you'll end up with something like所以你最终会得到类似的东西

{
    $errors = validateData($_POST);
    if ($errors) {
        echo generateErrorPage($errors);
        exit;
    }
    $result = createUserInDBAndSendEmail($_POST);
    showSuccessPage($result);
}

(That's very rough and ready for the sake of example). (为了举例,这非常粗略,准备好了)。


Don't divide the steps you need to do to complete the task up between different controllers.不要在不同的控制器之间划分完成任务所需的步骤。 Then you only have one URL to worry about.那么您只需要担心一个 URL。

You can divide the functions up between different files and then include them.您可以在不同的文件之间划分功能,然后include它们include


The complete process for creating a user might require additional feedback from the client, eg创建用户的完整过程可能需要来自客户端的额外反馈,例如

  1. Client makes a request to the CreateUser controller客户端向 CreateUser 控制器发出请求
  2. Server creates a user in "unconfirmed" state, sends email to the user and returns an HTML page telling them to check their email服务器创建一个处于“未确认”状态的用户,向用户发送电子邮件并返回一个 HTML 页面,告诉他们检查他们的电子邮件
  3. User clicks a link in the email to make a request to the ConfirmEmail conroller用户单击电子邮件中的链接向 ConfirmEmail 控制器发出请求
  4. Server changes the user to "confirmed" and returns a logged in homepage with a welcome message服务器将用户更改为“已确认”并返回带有欢迎消息的登录主页

… and in that case it would have multiple controllers since you need the user to make two different requests. ……在这种情况下,它将有多个控制器,因为您需要用户发出两个不同的请求。

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

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