CodeIgniter Forums
How to Count the number of receipients and insert in the database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to Count the number of receipients and insert in the database (/showthread.php?tid=70092)

Pages: 1 2


How to Count the number of receipients and insert in the database - CharlesK - 02-19-2018

Hello guys, I am able to insert data in the database in the view below, my current problem is i want to save the total number of receivers, like lets say i am sending an sms to three receivers, i would like to save that 3 in a column called receipientcount in the database. Anyone with ideas, please help: here is the view:
Code:
<link href="<?php echo base_url() ?>assets/css/bootstrap-timepicker.min.css" rel="stylesheet">
<link href="<?php echo base_url() ?>assets/css/datepicker3.css" rel="stylesheet">
<link href="<?php echo base_url() ?>assets/tagsinput/bootstrap-tagsinput.css" rel="stylesheet">

<script type="text/javascript" src="<?php echo base_url() ?>assets/js/bootstrap-timepicker.js"></script>
<script type="text/javascript" src="<?php echo base_url() ?>assets/js/bootstrap-datepicker.js"></script>

<script type="text/javascript" src="<?php echo base_url() ?>assets/js/typeahead.js/bloodhound.min.js"></script>
<script type="text/javascript" src="<?php echo base_url() ?>assets/js/typeahead.js/typeahead.bundle.min.js"></script>
<script type="text/javascript" src="<?php echo base_url() ?>assets/js/typeahead.js/typeahead.jquery.min.js"></script>

<script type="text/javascript" src="<?php echo base_url() ?>assets/tagsinput/bootstrap-tagsinput-angular.min.js"></script>
<script type="text/javascript" src="<?php echo base_url() ?>assets/tagsinput/bootstrap-tagsinput.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-maxlength/1.7.0/bootstrap-maxlength.min.js"></script>


<button class="btn btn-primary btn-xs pull-right" data-toggle="modal" data-target="#myModal">
   Send Message
</button>
<style type="text/css">
   .modal-backdrop{z-index: 1200;}
   .modal{z-index: 1201;}
   .tt-dropdown-menu{z-index: 1203; border: solid 1px #CCC; margin-right: 0; margin-left: 0; background-color: #fff; border-color: #ddd; border-width: 1px; border-radius: 4px 4px 0 0; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; -webkit-box-shadow: none; box-shadow: none;}
   .tt-suggestions{}
</style>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="z-index: 9999;">
   <div class="modal-dialog">
       <div class="modal-content">
           <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
               <h4 class="modal-title" id="myModalLabel">Send Message</h4>
           </div>
           <div class="modal-body">
               <form name="SendSMSForm" id="SendSMSForm" action="<?php echo site_url();?>/sms/sms_form"  class="form-horizontal" role="form" method="post">
                   <div class="form-group">
                       <label class="col-sm-2 control-label">Sender</label>
                       <div class="col-sm-10">
                           <p class="form-control-static"><label class="label label-primary">Inclusion</label></p>
                       </div>
                   </div>
                   <div class="form-group">
                       <label for="receiver" class="col-sm-2 control-label">To</label>
                       <div class="col-sm-10">
                           <input class="form-control" id="input-tag" type="text" data-role="tagsinput" placeholder="" name="receiver" >
                           <p class="help-block">
                               <small>
                                   Enter name of person/group in address-book E.g John, Family, Workmates
                               </small>
                           </p>
                       </div>
                   </div>
                   <div class="form-group">
                       <label for="message" class="col-sm-2 control-label">Message</label>
                       <div class="col-sm-10">
                           <textarea class="form-control" name="body" rows="3"  id="body"></textarea>
                          <p>
                           <span id="remaining">160 characters remaining</span>
                           <span id="messages">1 page(s)</span>
                           </p>        
                       </div>
                   </div>

                   <a data-toggle="collapse" data-parent="#accordion" href="#SendSMSOptions">
                       <span class="glyphicon glyphicon-plus"></span> Options
                   </a>
                   <div id="SendSMSOptions" class="panel-collapse collapse in">
                       <div class="panel-body">
                           <div class="form-group">
                               <label for="Schedule" class="col-sm-2 control-label">Schedule Send</label>
                               <div class="col-sm-10">
                                   <br />
                                   <div class="input-group date">
                                       <input data-provide="datepicker" name="datepicker" type="text" class="form-control"><span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
                                   </div>
                                   <div class="input-group bootstrap-timepicker">
                                       <input id="timepicker" data-template="modal"  name="timepicker" type="text" class="form-control">
                                       <span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
                                   </div>
                               </div>
                           </div>
                       </div>
                   </div>

               </form>
           </div>
           <div class="modal-footer">
               <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
               <button type="button" class="btn btn-primary" id="SendSMS">Send Now</button>
           </div>
       </div>
   </div>
</div>

<script>
   //we are one
   $(document).ready(function() {
       $('.collapse').collapse();
       $('#timepicker').timepicker({showMeridian: false, minuteStep: 15, defaultTime: 'current'});

       $('.input-group.date').datepicker({
           format: 'mm/dd/yyyy',
           startDate: '-3d',
           autoclose: true,
           todayHighlight: true
       });
       $(document).ready(function(){
           var $remaining = $('#remaining'),
               $messages = $remaining.next();

           $('#body').keyup(function(){
               var chars = this.value.length,
                   messages = Math.ceil(chars / 160),
                   remaining = messages * 160 - (chars % (messages * 160) || messages * 160);

               $remaining.text(remaining + ' characters remaining');
               $messages.text(messages + ' page(s)');
           });
       });
   $(document).ready(function() {
       $("#SendSMS").click(function(event) {
           event.preventDefault();
           console.log($('#SendSMSForm').serializeArray())
               jQuery.ajax({
                       type: "POST",
                       url: "<?php echo base_url('sms/sms_form') ?>",
                       dataType: 'html',
                       data:  $('#SendSMSForm').serializeArray(),
                       success: function(data){
                              alert(data);
                             }
                         });
                           return false;
                   });
    });
   $(document).ready(function() {
       var smslog = new Bloodhound({
           datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
           queryTokenizer: Bloodhound.tokenizers.whitespace,
           prefetch: "<?php echo base_url('tags/get_tags');?>"
       });
           smslog.initialize();
               elt = $('#input-tag');
               elt.tagsinput({
               itemValue: 'smsid',
               itemText: 'receiver',
               typeaheadjs: {
                   name: 'smslog',
                   displayKey: 'text',
                   source: smslog.ttAdapter()
               }
            });
           });

   });
</script>



RE: How to Count the number of receipients and insert in the database - Wouter60 - 02-19-2018

Quote:I am able to insert data in the database in the view below
What do you mean? I don't see any database actions inside your view. In fact, that's good news, because database operations don't belong inside views. That's what models are for.
Please, explain how your view interacts with the controller, and if you have a model for saving stuff into your database.


RE: How to Count the number of receipients and insert in the database - CharlesK - 02-19-2018

sorry for that, I want to insert the number of pages in a message, like the message text field in the view is able to count the number of words and limit them to 160 then count that as page 1, the same way when you send a sms , now i want to store that page count. When you look keenly in the script you will see how what am talking about: here is my controller and model respectively

Code:
<?php defined( 'BASEPATH' ) OR die('No direct script access allowed!');

class sms extends CI_Controller {

   public function __construct() {
       parent::__construct();
       $this->config->load('facebook');
       $this->load->library('form_validation');
       $this->load->library('encrypt');
       $this->load->model('countries');
       $this->load->helper('text');
       $this->load->model('sms_model');
   }
       public function index() {
   }

   public function sms_form(){


       $sender = ('sender');
       $numberofpages = '';
           if(isset($_POST['messages'])){
               $numberofpages .= $_POST['messages'];
           };
       $body = $this->input->post('body');
       $sendscheduletime = '';
           if(isset($_POST['datepicker'])){
               $sendscheduletime .= $_POST['datepicker'];
           }
           if(isset($_POST['timepicker'])){

               $sendscheduletime .= ' ' .$_POST['timepicker'];
           };
       $route = $this->input->post('route');
       $category = $this->input->post('category');
       $recepientscount = $this->input->post('recepientscount');
       $receiver = $this->input->post('receiver');
       $sumcharge = $this->input->post('sumcharge');
       $sentstatus = $this->input->post('sentstatus');

       $sms_data = array(
           'body' => $body,
           'numberofpages' => $numberofpages,
           'sender' => 'inclusion',
           'sendscheduletime' => $sendscheduletime,
           'route' => $route,
           'category' => $category,
           'recepientscount' => $recepientscount,
           'receiver' => $receiver,
           'sumcharge' => $sumcharge,
           'sentstatus' => $sentstatus,
       );
       var_dump($sms_data);die;
        $this->sms_model->insert($sms_data);
        echo "Message Send Successfully....!!!!";
       // $this->load->view('modal_send_sms'); // Reloading after submit.
       // $this->redirect('outbox');
   }
}

/* End of file sms.php */

Code:
<?php defined( 'BASEPATH' ) OR die('No direct script access allowed!');

class sms_model extends CI_Model {
    function __construct() {
       // Call the Model constructor
        parent::__construct();
    }

    function insert( $sms_data ) {
    $this->db->insert('smslog', $sms_data);
        
    }

}

/* End of file sms.php */



RE: How to Count the number of receipients and insert in the database - Wouter60 - 02-19-2018

There is no field named "messages" in your form, so $_POST['messages'] will always be empty.
Create a hidden field with the name "messages" and let jQuery assign a value to it before the AJAX call is done.
I'm just curious: why $_POST and $this->input->post() in the same function? $this->input->post() is CI's way of handling $_POST.


RE: How to Count the number of receipients and insert in the database - CharlesK - 02-19-2018

That is because I am picking two values and inserting them to the same column, that is why i have used both. Look at the label Message it has a span which contains the name = "messages"


RE: How to Count the number of receipients and insert in the database - Wouter60 - 02-19-2018

Did you check your console.log? Does the serialized array contain any key named 'messages' or 'recipientscount'?
What I'm trying to say is this: $('#SendSMSForm').serializeArray() will only store the values of form inputs (by name), not spans or labels.


RE: How to Count the number of receipients and insert in the database - CharlesK - 02-19-2018

ooooh, now i get, how do i get to insert the spans in the DB???


RE: How to Count the number of receipients and insert in the database - kirasiris - 02-19-2018

(02-19-2018, 12:59 PM)CharlesK Wrote: ooooh, now i get, how do i get to insert the spans in the DB???

its not possible .-. . To be able to send data trought out a form you must use inputs, there's no way around. Why dont you use inputs to send them to the database?


RE: How to Count the number of receipients and insert in the database - Wouter60 - 02-20-2018

Like I said before: create a hidden field and let jQuery assign a value to it before the AJAX call is done.

Next to the span element, create a hidden input:
Code:
<input type="hidden" name="message_count" id="message_count" value="0" />

Also, adjust your jQuery script that responds to keyup in the message textarea:
Code:
$('#body').keyup(function(){
    var chars = this.value.length,
        messages = Math.ceil(chars / 160),
        remaining = messages * 160 - (chars % (messages * 160) || messages * 160);

    $remaining.text(remaining + ' characters remaining');
    $messages.text(messages + ' page(s)');
       $('#message_count').val(messages);   //this line is new!
});

Now, remember that the POST value for the controller is message_count, not messages.


RE: How to Count the number of receipients and insert in the database - CharlesK - 02-20-2018

(02-19-2018, 07:22 PM)kirasiris Wrote:
(02-19-2018, 12:59 PM)CharlesK Wrote: ooooh, now i get, how do i get to insert the spans in the DB???

its not possible .-. . To be able to send data trought out a form you must use inputs, there's no way around. Why dont you use inputs to send them to the database?

00h, bacause i am not writing anything, its a function that counts the number of pages in a text.