简体   繁体   English

PHP MVC将表单值传递给控制器​​和模型

[英]PHP MVC passing form values to controller and model

I'm trying to send POST values to the controller and then pass it to the model in PHP but I'm not sure how to go about doing this. 我正在尝试将POST值发送到控制器,然后将其传递给PHP中的模型,但是我不确定如何执行此操作。

This part of the controller is to see if the user requests for a view like ?action=game . 控制器的这一部分是查看用户是否请求像?action=game这样的视图。 This works. 这可行。

But I'm trying to modify it to allow $_POST to be sent to it and then to the model. 但是我试图对其进行修改,以允许将$_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');
     }  
}

Here's what I got. 这就是我得到的。 In the registration form page, the action of the form is ?process=register but it doesn't work. 在注册表单页面中,表单的操作是?process=register但不起作用。

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

Get_view function determines what model to bind with the view Get_view函数确定要与视图绑定的模型

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);
}

Since the action of your form is ?process=register , then process is still in the $_GET superglobal. 由于表单的操作是?process=register ,因此process仍在$_GET超全局变量中。 What you can do to make it use post is add a hidden input field containing process. 要使它使用post,您可以做的是添加一个包含进程的隐藏输入字段。

With this: 有了这个:

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

The form is POST'ed to script.php?process=register so you have $_GET['process'] , not $_POST['process'] . 该表单已过POST到script.php?process=register因此您拥有$_GET['process']而不是 $_POST['process']

Try this instead: 尝试以下方法:

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

To have $_POST['process'] . 要有$_POST['process'] Alternatively, you could keep the "process" in the GET and switch your if statement to check $_GET instead of $_POST . 或者,您可以将“进程”保留在GET中,并将if语句切换为检查$_GET而不是$_POST

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

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