0

Hi

Im getting into a php mvc framework, available here (http://www.phpro.org/tutorials/Model-View-Controller-MVC.html#8), but they only explain their code, and dont offer much other advice on the basics of working inside a framework...

how would i go about posting a form?
working outside of any framework i usually just post the form to the same page, and include an

if(isset($_POST['save']))
    {
        //do something with post data
    }

how soes it work when you've got an htaccess file manipulating your urls and you're inside a framework?

thanks

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by speedovic
0

Hey.

You would just define a different action to process the form.

A controller typically has a method for every action that you can take. For example, if you were to call the URL localhost/form/show , a controller for "form" would be created, and the action "show" would be called. - In other words, the form::show method would be called.

For example (and excuse the length of this example... it kind of snow-balled on me), consider this controller:

<?php
class FormController //extends Controller
{
    protected $view;
    protected $model;

    /**
     * Initializes the Vew and the Model.
     */
    public function __construct()
    {
        $this->model = new FormModel();
        $this->view = new FormHtmlView($this->model);
    }

    /**
     * The "index" action.
     * Called by default if no action is defined.
     */
    public function index()
    {
        $this->show();
    }

    /**
     * The "show" action.
     * Simply instructs the View to display the form.
     */
    public function show()
    {
        $this->view->showForm();
    }

    /**
     * The "process" action.
     * Processes the form data, either sending it to the model to be
     * saved into the database, or displays errors if the required
     * fields are not present.
     */
    public function process()
    {
        $requiredFields = array('name', 'email');
        $data = array();
        $error = false;

        foreach($requiredFields as $_field)
        {
            if(!isset($_POST[$_field]) || empty($_POST[$_field]))
            {
                $error = true;
                $this->view->showProcessError("Field '{$_field}' needs to be filled.");
            }
            else
            {
                // Skipping any sort of validation, for the sake of
                // simplicity.
                $data[$_field] = trim($_POST[$_field]);
            }
        }

        if($error)
        {
            $this->view->showForm();
        }
        else
        {
            if($this->model->saveFormData($data))
            {
                $this->view->showSuccessMessage();
            }
            else
            {
                $this->view->showProcessError("Internal error. Failed to save the data into the database.");
            }
        }
        
    }
}
?>

This controller defines three actions: index, show and process.

Initially, this would show a HTML form, which may look something like this:

<form action="/form/process/" method="post">
    Name: <input type="text" name="name"><br>
    Email: <input type="text" name="email"><br>
    <input type="submit">
</form>

Your .htaccess file should rewrite the action URL into something like ?ctrl=form&action=process , which the index file of your PHP app could use to trigger the FormController::process method, which would then process the data and either insert it into the database or show the form again with an error message.

Am I making any sense?
(I haven't had any coffee yet xD)

0

hi

thats great, you totally make sense - more importantly I now understand why most of the urls passing through the index.php page will have a controller AND an action attached... :)

Thanks,

0

Looks great... I was searching the net for such a form to implement in MVC. Now I want to use the same concept but instead of sending data to the database I would like to retreive the same information. I created a model where I have my select query which works fine. but now I have issue with submit button to get data back from the sql-db and display it on screen . Which part should I change in the controller and HTML file. thx

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.