How to Create a Simple, Functional Contact Form Using Yii 2

by Ahmed Khan  June 21, 2016

In the previous installment of this series, I developed a Hello World app using Yii2. In this tutorial, I will create a database with a table which will save the contact form data, and connect the database to the Yii2 application.

Creating Contact Form in Yii2

Let’s first create a database and a table.

Related: How To Host Yii 2 Framework On Cloudways Using Composer

Creating the Database

Open PHPMyadmin and create a database. Name it yii2 to make it easy to identify and associate with the project. Now create a table with the following schema:

Connecting the App to the Database

Now that we have successfully created the tables in the database, let’s connect the Yii2 app to the newly created database. Head to the config folder in the Yii2 basic directory and open db.php.

db.php

Change the dbname from yii2basic to yii2, enter the username and password and save the file.

Change dbname

Since the connection to the database is now established, let’s now create a model that will perform various operations on the table.

Related: How To Use Elasticsearch With Yii 2

Creating the Model

Since I am creating a model for the Contact table, the name of the model class will be same as the table’s name. I will extend from \yii\db\ActiveRecord a helper class of Yii2, that will help in accessing and manipulating data in the database.

Go to the models folder, create a new file and name it Contact.php (make sure that the first letter is capital). Now paste the following code in it.

Inside this class, I first created a function called tablename that returns the name of the table. Next, I created a function which defines the set of rules in an array:

One thing you might have noticed is that I haven’t created any function to insert data into the table.  Since we have extended our model with ActiveRecord, we don’t need to write input functions. Now let’s create an action which will handle this model and the form.

Creating the Controller

Go to the controllers folder and open SiteController.php. First, add the model to the top

Now create a new function in class and name it actionForms. Pastes the following code in it:

Inside the function, first I created an instance of our Model:

and then I check whether the request is a post and then save the data in the table by using $model->save(),  a built-in function in ActiveRecord that is used for inserting data into a table.

Next, we check whether the form is submitted. And then I rendered the form’s view along with the model data.

If the request is not a post, The form’s view is rendered along with  $model along with it.

I am sending $model along with the render function because it will have the data when a user submits the form. If the data does not validated, then it will be sent back to the user to correct it. The $model will help the user view the submitted data along with the errors.

Now let’s create our form view.

Creating the View

Go to the views folder>>site folder, create a new view and name it forms.php. Paste the following code.

First,  there is the helpers class of Yii2, that we will be using in our views.

In the first part, the form checks whether the form is submitted or not. If the form is submitted, I than view the data provided along with confirmation.

In the else part, I have created a form which the user will use to send the message. The form has been created using the ActiveForm helper of Yii2.

Now let’s test the code by going to action ?r=site/form.  The contact form will be shown.

Contact Form

Try sending a message using the form.

Contact Form in Yii2

Once the message is sent, the form is successfully designed and configured.

Contact Form Message

Now check the database. You will see that a new record has been added to it.

New Record in Database

Conclusion

In this tutorial, we have created a form and saved its data in a database. In the next part of this series, I will demonstrate the versatility of Yiii2 through a to-do list using gii. Here is the complete list of topics covered in this series on Yii2.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...