将用户重定向到另一个页面后如何 运行 php 函数?
How to run php function after redirecting user to another page?
我正在构建一个用户填写的表单,然后将其保存到数据库中。然后我检索符合条件的用户,并将每个用户存储到另一个 table 中,并向他们发送电子邮件。
$userModel = new User();
$currentUser = $userModel->findUserById($user->id);
$requestModel = new Requests();
$success = $requestModel->saveRequest($currentUser->usr_id, $tagId, $title, $task, $fixed, $price, $hour, $quality, $multiple, $datetime, $postal, $cityId, $travel);
if($success){
$request = $requestModel->getUserLatestRequest($currentUser->usr_id);
if($request){
$user = new User();
$alluserids= $user->getAllSkillCityUserIds($cityId, $tagId);
$targetId = (array_column($alluserids, 'usr_id'));
//error_log("<pre>targetId".print_r($targetId,true)."</pre>");
foreach($targetId as $target) {
if($target == $currentUser->usr_id){
continue;
}
$lead = new RequestsLead();
$lead->addRequest($request->req_id, $request->req_userid, $target);
$contractor = $userModel->findUserbyId($target);
$nemail = new NotificationsEmail();
$nemail->sendGotRequest($contractor->usr_email, $contractor->usr_firstname);
}
}
$this->flash->success('<div data-toggle="notify" data-onload data-message="Thanks for using our service!." data-options="{"status":"success"}" class="hidden-xs"></div>');
$this->response->redirect($this->url->get(""));
}else{
$this->flash->error('<div data-toggle="notify" data-onload data-message="Sorry! Please try again." data-options="{"status":"danger"}" class="hidden-xs"></div>');
$this->response->redirect($this->url->get("request"));
}
当有很多用户时就会出现问题,并且此功能需要完成 运行ning 才能将用户重定向回带有闪现消息的页面。我该如何修改它,以便我首先将用户重定向回带有 Flash 消息的页面,然后 运行 php foreach 函数存储到数据库中并在之后发送电子邮件?
我尝试切换功能的顺序,但是一旦用户被重定向到闪现消息,php 功能就停止了。
您提到的内容需要某种方式让脚本保持 运行ning 超越发送回用户的响应。 PHP 不会以这种方式工作 - 您可以开始将内容写入输出缓冲区,但浏览器仍将等待直到返回整个响应。
相反,您应该考虑一些进程来临时存储您需要的信息,并异步处理这些信息。例如将它们存储到数据库和 运行 一个 cron 脚本或守护进程来执行此操作。
PHP 是一个 server side scripting language。它本质上(但不完全)是无状态的。
这意味着当加载 PHP 页面时,它会在服务器上执行所需的 PHP 代码,然后将响应发送到浏览器。 页面发送到客户端后,如果不重新调用服务器,就无法重新执行 PHP 代码。
在您的例子中,您将客户重定向到一个新的 PHP 页面。太好了,但是新的 PHP 页面是服务器上 运行 的新脚本,它不知道上一页在做什么。
为了在页面加载后执行 PHP 代码,您需要使用 AJAX 向 PHP 服务器发送请求以在如果您想在不再次重定向用户的情况下执行此操作,或者在显示 'flash' 消息后再次重定向用户。
请注意,使用 AJAX 您还可以使用原始页面 - 无需任何重定向 - 执行此请求并显示即显消息(同时!)。
有关页面之间共享数据的更多信息:
- How do I pass data between pages in PHP?
- Passing POST data from one web page to another with PHP
- Transfer variables between PHP pages
有关 AJAX 请求和 PHP 的更多信息:
- Making Ajax service calls with PHP, jQuery, and JSON
(非 Whosebug Link)
- Ajax 开发新手指南
PHP(非 Whosebug Link)
我正在构建一个用户填写的表单,然后将其保存到数据库中。然后我检索符合条件的用户,并将每个用户存储到另一个 table 中,并向他们发送电子邮件。
$userModel = new User();
$currentUser = $userModel->findUserById($user->id);
$requestModel = new Requests();
$success = $requestModel->saveRequest($currentUser->usr_id, $tagId, $title, $task, $fixed, $price, $hour, $quality, $multiple, $datetime, $postal, $cityId, $travel);
if($success){
$request = $requestModel->getUserLatestRequest($currentUser->usr_id);
if($request){
$user = new User();
$alluserids= $user->getAllSkillCityUserIds($cityId, $tagId);
$targetId = (array_column($alluserids, 'usr_id'));
//error_log("<pre>targetId".print_r($targetId,true)."</pre>");
foreach($targetId as $target) {
if($target == $currentUser->usr_id){
continue;
}
$lead = new RequestsLead();
$lead->addRequest($request->req_id, $request->req_userid, $target);
$contractor = $userModel->findUserbyId($target);
$nemail = new NotificationsEmail();
$nemail->sendGotRequest($contractor->usr_email, $contractor->usr_firstname);
}
}
$this->flash->success('<div data-toggle="notify" data-onload data-message="Thanks for using our service!." data-options="{"status":"success"}" class="hidden-xs"></div>');
$this->response->redirect($this->url->get(""));
}else{
$this->flash->error('<div data-toggle="notify" data-onload data-message="Sorry! Please try again." data-options="{"status":"danger"}" class="hidden-xs"></div>');
$this->response->redirect($this->url->get("request"));
}
当有很多用户时就会出现问题,并且此功能需要完成 运行ning 才能将用户重定向回带有闪现消息的页面。我该如何修改它,以便我首先将用户重定向回带有 Flash 消息的页面,然后 运行 php foreach 函数存储到数据库中并在之后发送电子邮件?
我尝试切换功能的顺序,但是一旦用户被重定向到闪现消息,php 功能就停止了。
您提到的内容需要某种方式让脚本保持 运行ning 超越发送回用户的响应。 PHP 不会以这种方式工作 - 您可以开始将内容写入输出缓冲区,但浏览器仍将等待直到返回整个响应。
相反,您应该考虑一些进程来临时存储您需要的信息,并异步处理这些信息。例如将它们存储到数据库和 运行 一个 cron 脚本或守护进程来执行此操作。
PHP 是一个 server side scripting language。它本质上(但不完全)是无状态的。
这意味着当加载 PHP 页面时,它会在服务器上执行所需的 PHP 代码,然后将响应发送到浏览器。 页面发送到客户端后,如果不重新调用服务器,就无法重新执行 PHP 代码。
在您的例子中,您将客户重定向到一个新的 PHP 页面。太好了,但是新的 PHP 页面是服务器上 运行 的新脚本,它不知道上一页在做什么。
为了在页面加载后执行 PHP 代码,您需要使用 AJAX 向 PHP 服务器发送请求以在如果您想在不再次重定向用户的情况下执行此操作,或者在显示 'flash' 消息后再次重定向用户。
请注意,使用 AJAX 您还可以使用原始页面 - 无需任何重定向 - 执行此请求并显示即显消息(同时!)。
有关页面之间共享数据的更多信息:
- How do I pass data between pages in PHP?
- Passing POST data from one web page to another with PHP
- Transfer variables between PHP pages
有关 AJAX 请求和 PHP 的更多信息:
- Making Ajax service calls with PHP, jQuery, and JSON (非 Whosebug Link)
- Ajax 开发新手指南 PHP(非 Whosebug Link)