PHP MVC 将表单值传递给 Controller ​​和模型

标签 php model-view-controller model

我正在尝试将 POST 值发送到 Controller ,然后将其传递到 PHP 中的模型,但我不确定如何执行此操作。

Controller 的这一部分是为了查看用户是否请求像?action=game这样的 View 。这有效。

但我正在尝试修改它以允许将 $_POST 发送给它,然后发送给模型。

function __construct()
{
    if(isset($_GET['action']) && $_GET['action']!="" )
     {
         $url_view = str_replace("action/","",$_GET['action']);
         if(file_exists("views/" . $url_view . ".php" ))
         {
                $viewname = $url_view;
                $this->get_view($viewname . ".php");
          }
          else
          {
               $this->get_view('error.php');
          }
     } 
     else 
     {
         $this->get_view('home.php');
     }  
}

这就是我得到的。在注册表单页面中,表单的操作是 ?process=register 但不起作用。

if(isset($_POST['process']) == 'register)
{
    $this->get_view('register.php')
}

Get_view 函数确定与 View 绑定(bind)的模型

function get_view($view_name)
{
    $method_name = str_replace(".php","",$view_name);
    if(method_exists($this->model,$method_name))
    {
       $data = $this->model->$method_name();
    } else {
      $data = $this->model->no_model();
    }
      $this->load->view($view_name,$data);
}

最佳答案

由于表单的操作是 ?process=register,因此 process 仍然位于 $_GET 超全局中。要使其使用 post,您可以做的就是添加一个包含进程的隐藏输入字段。

这样:

<form method="post" action="script.php?process=register">

表单已 POST 到 script.php?process=register,因此您有 $_GET['process']没有 $_POST['进程']

试试这个:

<form method="post" action="script.php">
<input type="hidden" name="process" action="register" />

拥有$_POST['process']。或者,您可以将“进程”保留在 GET 中,并切换 if 语句以检查 $_GET 而不是 $_POST

关于PHP MVC 将表单值传递给 Controller ​​和模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10746396/

相关文章:

具有 Observer/Observable 的 Java MVC 模式

model-view-controller - Yii 2 从 View 访问 Controller 的更快方法

database - 无法声明类 App\User,因为该名称已在我的浏览器中使用

javascript - 模型渲染内 View : toJSON is not a function

javascript - 我的 ajax 调用返回带有 json 数据的 html

php - 我如何使用 laravel 获得多个计数查询?

php - 如何在 Opencart 中为自定义、动态生成的页面创建对 SEO 友好的链接?

javascript - jQuery MVC 整体教程或书籍

model - 如何在 extjs 4 存储中指定型号名称?

php - 动态 Mysql 查询 & 通过 `GET` 传递多个值