redirect to a new page on submitting a form

Hi,

I want to redirect to a new page after submitting a form.Can anybody tell me how i can go about it?

See redirect for details.




    public function actionCreate()

    {

        $model = new Form;


        if (isset($_POST['contactForm']))

        {

            $model->attributes = $_POST['contactForm'];

            if ($model->save())

                $this->redirect(array('view', 'id' => $model->id));

        }


        $this->render('create', array(

            'model' => $model,

        ));

    }



Cheers,

Matt

Matt,

Just for the record i am completely new at this so u might have to explain it in a bit of detail.

This code will redirect me to the view page, but in my case, i have to create a NewPage and then redirect to it. This NewPage will have another form in it. Where am i supposed to save this NewPage and how do i redirect to it?

I’ll assume these two forms are related and will be kept in the same view folder.

Let’s call the forms orderForm & orderConfirmationForm. Place both of them in views/order folder.

Flow:

[list=1]

[*]User arrives at createOder (controller action) which displays orderForm

[*]User successfully submits order

[*]User is redirected to OrderConfirmation (controller action) which displays orderConformationForm

[*]User confirms order

[*]User is redirected to view (controller actionView). This view should display the order he/she just placed

[/list]




    /**

     * Creates a new oder.

     * This is the 1st form that the uset will fill out

     * If creation is successful, the browser will be redirected to the 'orderConfirmation' page.

     */

    public function actionCreateOrder()

    {

        // Create a new model -CForm, CActiveRecord etc...

        $model = new Order();


        // Go in here if the form is submitted

        if (isset($_POST['order']))

        {

            // Assign all the fields that the user entered

            $model->attributes = $_POST['order'];

            

            // Save the model and validate it

            // If validation fails, save will return false and the render method

            // at the end of this method will be invoked. All errors will be displayed

            // to user

            

            // If save successful, it will redirect to your 2nd form (orderConformationForm)

            if ($model->save())

            {

                // This 1st param of this method specifies the action/destination.

                // Notice, that it's being redirected to the method below - actionOrderConfirmation

                // Don't include 'action' in the method.

                $this->redirect(array('orderConfirmation', 'id' => $model->id));

            }

        }


        // This view will be called when the form is first displayed AND when the

        // user submits the form and there are validation errors.

        $this->render('orderForm', array(

            'model' => $model,

        ));

    }

    

    /**

     * Users are redirected here after the order has been successfully submitted.

     * @param <type> $id The id of the Order

     */

    public function actionOrderConfirmation($id)

    {

        // Load the Order

        $model = $this->loadModel($id);


        // Check the form was submitted

        if (isset($_POST['confirmation']))

        {

            // Assign the variables.

            // Maybe in your 2nd form you need the user to check a box saying the order 

            // is correect before they submit

            $model->attributes = $_POST['confirmation'];

            

            // Validate and save.

            // If valid, the Order will be saved and the user will be redirected 

            // to a view (views/orders/view), possibly confirming their order 

            // has been placed.

            if ($model->save())

            {

                // This will redirect the user to (views/orders/view). Note that 

                // by default, the view is relative to the current controller's

                // associated views.

                // For example: OrderController is associated with views/order/

                $this->redirect(array('view', 'id' => $model->id));

            }

        }


        // This view will be called when the form is first displayed AND when the

        // user submits the form and there are validation errors.

        $this->render('orderConformationForm', array(

            'model' => $model,

        ));

    }



Note that in Yii, a controller has associated views. This rule can obviously be broken and any controller may call any view, but for default purposes, OrderController is associated with views/order folder and it’s views.

This way you can call $this->render(‘orderConformationForm’, array(‘model’ => $model,…) from the controller and you know that it’s calling the view located at views/order/orderConformationForm. The same applies to redirects.

Hope this answers your questions.

Cheers,

Matt

Yes it did help… Thanks a million…

just A twist in the requirement…

i need to redirect it to the form of some other model but want the values which user inputted in the first form. i am able to connect it to the form of other model but not able to get the values inputted in the prev form… any advice how to go about??

There are two standard ways to achieve this. The first is to store the data in the session and then retrieve it in the new controller. I would recommend this route if you have a simple(ish) form with texboxes and the like.

For a more robust and complicated form, you can create a class similar to Java’s Data Transfer Object (DTO). This class would simply have accessors/mutators (get & set functions) that will enable you to write and read the data. This option, you again have two options for data persistence between requests.

A.) Have a method that stores the data to the session (saveToSession() & getFromSession()) Possibly implement PHP’s serialize interface.

B.)Store the data in a DB and retrieve it when needed. This option is easy to implement - you should extend your DTO from CActiveRecord & add a table to your DB with the necessary fields. Use the Gii tool to help you.

On your first form, create the object, save it, pass the newly created id to the next controller, retrieve the object and the data.

Let me know which route you decide to go and I can help you implement it.

Cheers,

Matt

Hi matt,

I would like to go with the session method. can you please elaborate on it or direct me to some link?.

Thanks

If you’ll be redirecting to the new page directly, without making another request in between, you can use flash data. See this article for how to use. Note that the data set in flash is only available for this and next request.

If you’ll be making a few requests in between displaying the new form, you’ll need to store the data in the session and then delete it after you have accessed it. This option allows you to collect the data, do a few requests, and then access the data at a later time. See the docs for how ti implement this.

Matt

Thanks matt…

Will try that… :)