Form in Magento

Hello All,
Many of you who are working in Magento might have come accross the need to make custom form for getting user data.
I am writing here an example to display form (with validation) for submitting basic info and send a mail on the email submitted.
Suppose the from need to be displayed at this url -> http://youmagentohost/formurl/
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
 
$url = Mage::getBaseUrl().'requestfrm';
//below code for sending mail after form is submitted
//start
if(isset($_POST) && !empty($_POST['request_flag'])):
    $params = $this->getRequest()->getParams();       //Fetch submited params
    //print_r($params);
    // message start
    $message = ''
 
    if(!empty($params)):
    $message = "
    <html>
    <head>
      <title>Customer details</title>
    </head>
        <body>
        <table border='0' cellpadding='4' cellspacing='0' width='90%'>
        <tbody>
        <tr>
          <td align='center' colspan='2' height='30' >Customer Details</td>
        </tr>
        <tr>
          <td align='right' width='40%'><b>Name:</b></td>
          <td width='55%'>".$params['name']."</td>
        </tr>
        <tr>
          <td align='right' width='40%'><b>Email Address:</b></td>
          <td width='55%'>".$params['email']."</td>
        </tr>
        <tr>
          <td align='right' width='40%'><b>Phone Number:</b></td>
          <td width='55%'>".$params['phone']."</td>
        </tr>
        <tr>
          <td align='right' width='40%'><b>Comments:</b></td>
          <td width='55%'>".$params['comment']."</td>
        </tr>
        </tbody>
        </body>
    </html>";
    endif
 
    $to = Mage::getStoreConfig('contacts/email/recipient_email'); //get email address set from site admin
 
    $dt = date('d-m-Y');
    // subject
    $subject = "Customer details submitted on date $dt";
    // message end
    $mail = new Zend_Mail();
    $mail->setBodyHtml($message);
    $mail->setFrom($params['email'], $params['name']);
    $mail->addTo($to, 'Site Admin');
    $mail->setSubject($subject);
    try {
      if($mail->send())
      {
        Mage::getSingleton('core/session')->addSuccess('Mail sent successfully. We will contact you shortly');
      }
    }
    catch(Exception $ex) {
        Mage::getSingleton('core/session')->addError('Unable to send email.');
    
 
endif;
//end
?>
<div align="left">
<div>
<form action="<?php echo $url ?>" id="requestForm" method="post">
    <div>
    <div>
        <div>
        <h3 class="txt-blue"><?php echo Mage::helper('contacts')->__('Information') ?></h3>
        </div>
        <ul class="form-list">
            <li>
                <label class="required"><?php echo $this->__('Name') ?>&nbsp;<font color="red">*</font></label>
                <div class="input-box">
                    <input name="name" id="name" title="name" value="" class="small required-entry" type="text" />
                </div>
            </li>
            <li>
                <label class="required"><?php echo $this->__('Email Address') ?>&nbsp;<font color="red">*</font></label>
                <div class="input-box">
                    <input name="email" id="email" title="email" value="" class="small required-entry validate-email" type="text" />
                </div>
            </li>
            <li>
                <label><?php echo $this->__('Phone Number') ?></label>
                <div class="input-box">
                    <input name="phone" id="phone" title="phone" value="" type="text" />
                </div>
            </li>
            <li>
                <label for="comment"><?php echo $this->__('Comment') ?></label>
                <div class="input-box">
                    <textarea name="comment" id="comment" title="" cols="5" rows="3"></textarea>
                </div>
            </li>
        </ul>
    </div>
    </div>
    <div style="margin:0">
        <p style="padding-right:10px;"><?php echo $this->__("<font color='red'>*</font> Required Fields") ?></p>
        <div style="padding-right:10px;"><button type="submit" title="<?php echo $this->__('Submit') ?>" ><span><?php echo $this->__('Submit') ?></span></button></div>
    </div>
    <input type="hidden" name="request_flag" value="1"/>
</form>
<script type="text/javascript">
//<![CDATA[
    var contactForm = new VarienForm('requestForm', true);
//]]>
</script>
</div>
</div>

How to check if Magento is running in production mode or developer mode

If you want to know. your Magento 2 website is running on which environment you can run below command on terminal and very easily you can kn...

Popular Posts

Posts