Codeigniter is an MVC based on PHP scripting language, MVC stands for Model View Controller. Small description about model view controller given below, take a look,
Model: Model used for the database, where we write queries code.
View: View is for design files or view files of our project.
Controller: Controllers are used for written functionality of the project or working of the project defined by some functions.
Smarty:
Smarty is a PHP template engine which uses for templating or designing the pages. Smarty may be an example engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. this suggests that PHP code is application logic, and is separated from the presentation.
Main Goal of Smarty
- clean partition of introduction from application code
- PHP backend, Smarty demonstrate frontend
- supplement PHP, not supplant it
- quick improvement/organization for software engineers and architects
- snappy and easy to keep up
- grammar easy to know, no PHP information required
- adaptability for custom improvement
- security: protection from PHP
- free, open supply
1. First, create a design and adding some javascript code for multiple fields like this
1 |
<form id="formShare" data-toggle="validator" class="form-horizontal" method="POST" action="YOUR_SITE_URL{$smarty.server.REQUEST_URI}"> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<strong> <span style="color: #339966">/* this is used for getting same page URL "ID" */</span></strong> {$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI} <div class="panel-body"> <div class="row"> <div class="col-md-6"> <div class="form-group"> <div class="col-md-4"> <label for="name0">Contact Name<span>*</span></label> </div> <div class="col-md-8"> <input required id="name0" name="emailto[0][name]" type="text" value="" placeholder="Contact Name" class="form-control input-md"> <div class="help-block with-errors"></div> </div> </div> </div> <div class="col-md-6"> <div class="form-group"> <div class="col-md-2"> <label for="email0">Email<span>*</span></label> </div> <div class="col-md-10"> <div class="input-group"> <input required id="email0" name="emailto[0][email]" type="email" value="" placeholder="Email" autocomplete="off" class="form-control input-md email_a"> <span class="input-group-btn"> <button class="btn btn-primary" type="button" title="Add More Contacts" id="addRow"><i class="fa fa-plus"></i></button> </span> </div> <div class="help-block with-errors"></div> </div> </div> </div> </div> <div id="nameEmailRow"></div> <div class="row"> <div class="col-md-12"> <div class="form-group"> <div class="col-md-2"> <label for="subject">Subject<span>*</span></label> </div> <div class="col-md-10"> <input required id="subject" name="subject" type="text" value="" placeholder="Subject" class="form-control input-md"> <div class="help-block with-errors"></div> </div> </div> </div> </div> <footer class="panel-footer"> <input type="submit" name="share" value="Share" class="btn btn-primary" id="submit"/> </footer> </div> </form> |
JS or javascript code for Adding multiple fields by using that plus button.
Also using SMARTY template engine, check below code
On SMARTY all <script> tags are written in {literal}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
{literal} <script type="text/javascript"> $(document).ready(function() { var i=0; $("#formShare").on("click", "#addRow", function() { i++; var rowHTML='<div class="row" id="nameEmailRowDiv'+i+'"><div class="col-md-6"><div class="form-group"><div class="col-md-4"><label for="name'+i+'">Contact Name<span>*</span></label></div><div class="col-md-8"><input id="name'+i+'" name="emailto['+i+'][name]" type="text" placeholder="Contact Name" required class="form-control input-md"><div class="help-block with-errors"></div></div></div></div><div class="col-md-6"><div class="form-group"><div class="col-md-2"><label for="email'+i+'">Email<span>*</span></label></div><div class="col-md-10"><div class="input-group"><input required id="email'+i+'" name="emailto['+i+'][email]" type="email" placeholder="Email" autocomplete="off" class="form-control input-md email_a" value=""><span class="input-group-btn"><button rel="'+i+'" class="btn btn-danger" type="button" title="Remove Contact" id="removeRow"><i class="fa fa-minus"></i></button></span></div><div class="help-block with-errors"></div></div></div></div></div>'; $("#nameEmailRow").append(rowHTML); }); $("#formShare").on("click", "#removeRow", function() { var row_id=$(this).attr('rel'); $("#nameEmailRowDiv"+row_id).remove(); }); }); </script> {/literal} <span style="color: #339966"><strong>//After that go to your controller function and fetch the entered emails using below code,</strong></span> if( $this->input->post('share') ) { $post = $this->input->post(); if( !empty($post['emailto']) && isset($post['emailto'][0]) && $post['emailto'][0]['name'] && $post['emailto'][0]['email'] ) { $subject = $post['subject'] ? $post['subject'] : 'MAIL_SUBJECT'; <span style="color: #339966"><strong>//this use for, access loop data from outside the loop</strong></span> $p =array(); <span style="color: #339966"><strong>//this loop gives you emmail data which entered by the user</strong></span> foreach( $post['emailto'] as $k => $e ) { if( !$e['name'] || !$e['email'] ) unset( $post['emailto'][$k] ); $p[]=$post['emailto'][$k]['email']; } <span style="color: #339966"><strong>//this gives you one by one mail IDs</strong></span> $pp='"'.implode(" , ",$p).'"'; $from_email = "MAIL_ADDRESS"; $to_email = implode(" , ",$p); $config=array( 'charset'=>'utf-8', 'wordwrap'=> TRUE, 'mailtype' => 'html' ); $this->data['template_data'] = $template_data; <span style="color: #339966"><strong>//if you want to sent any file or template </strong></span>$mesg = $this->cismarty->view('VIEW_FILE', $this->data, true); $this->load->library('email'); $this->email->from($from_email, 'MAIL_NAME'); $this->email->to($to_email); $this->email->subject($subject); $this->email->message($mesg); if($this->email->send()){ echo "<script>alert('Sent')</script>"; }else{ <span style="color: #339966"><strong>//this function shows you if mail code have any error or any field is missing.</strong></span> echo $this->email->print_debugger(); } } } |