简体   繁体   中英

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.

This part of the controller is to see if the user requests for a view like ?action=game . This works.

But I'm trying to modify it to allow $_POST to be sent to it and then to the model.

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.

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

Get_view function determines what model to bind with the 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. What you can do to make it use post is add a hidden input field containing process.

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'] .

Try this instead:

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

To have $_POST['process'] . Alternatively, you could keep the "process" in the GET and switch your if statement to check $_GET instead of $_POST .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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