我正在尝试将 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/