buy viagra valium online

How to add custom field in Contact Form?

Posted Posted by Loic FARGE in Magento     Comments No comments
Oct
4

Hi everybody,

Today I will write for you a small article on “How to add custom field in your contact form?”. In fact this feature is pretty simple to implement but well. We will need to edit two files for this feature. The first one is the contact form template file and the second is the email template file to add your information inside. Let’s start!

Open the file contacts/form.phtml located in //. You can find the list of fields bellow.

Now it is pretty simple. The only thing you will have to do is adding your new custom field. By default, Magento uses classes “wide” for full row fields and “fields” for half row fields. Try to keep the same structure to avoid much css work. In our sample, we will add a field called “Country” next to the “Phone number” field. Nothing much interesting but it is only for sample purpose. See code bellow:

  • Here you are done. You can check your form to see that “Country” field has been added into.

    Well, we are not done yet as if you try to submit your form, it will not send the information in the email. Why?

    Open the file “app/locale//template/email/contact_form.phtml”. You will find the following content on it:

     
     
    Name: {{var data.name}}
    E-mail: {{var data.email}}
    Telephone: {{var data.telephone}}
    
    Comment: {{var data.comment}}
    

    You can see that content is added in the template by using data variable which is given to the template by the controller post action. So the only thing we need to do is add our country field there like that:

     
     
    Name: {{var data.name}}
    E-mail: {{var data.email}}
    Telephone: {{var data.telephone}}
    Country: {{var data.country}}
    
    Comment: {{var data.comment}}
    

    You are done! Now your contact form will get one more field sent by email as usual.

    Thanks for your reading and hope you enjoy this new article.

    404