CodeIgniter Forums

Full Version: [solved] Delay in ajax response time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi When I click on my approve button it sends a confirmation letter to the user and adds the members information to the main members table from the members temp table.

Every thing works fine but the email is causing about a 10 second delay for my ajax. Once the accout approve it refreshes the div but takes 10 second to refresh because of email library.

How to make that it faster.


Code:
<script type="text/javascript">
$(document).ready(function() {
    $(document).on('click', '#button-approve', function() {
        $.ajax({
            url : "<?php echo base_url(); ?>admin/members/dashboard/approve_member_login",
            type : "POST",
            dataType : "json",
            data : {
                "approve_member" : true,
                "member_temp_id" : $(this).attr('data-id')
            },
            success : function(data) {
                if (data['success'] = true)
                {
                    $('#new_member_approve').load(document.URL + ' #new_member_approve');
                }
            },
          
       });
    });
});        
</script>



PHP Code:
public function approve_member_login() {

    
$json = array();

    if (
$this->input->post('approve_member')) {

        
$newmembers_info $this->get_temp_members($this->input->post('member_temp_id'));

        
$config = Array(
            
'protocol' => 'smtp',
            
'smtp_host' => 'ssl://smtp.googlemail.com',
            
'smtp_port' => 465,
            
'smtp_user' => 'email',
            
'smtp_pass' => 'password',
            
'mailtype'  => 'html'
            
'charset'   => 'iso-8859-1'
        
);


        
$this->load->library('email'$config);
        
$this->email->set_newline("\r\n");

        
$this->email->from('myemail''Admin');
        
$this->email->to($newmembers_info['email']);
        
$this->email->subject('Account Approved');
        
$this->email->message('Hi, Just to confirm that your account has now been approved at Admin Area');

        
$this->email->send();

        
$insert_data = array(
            
'username' => $newmembers_info['username'],
            
'email' => $newmembers_info['email'],
            
'password' => $newmembers_info['password'],
            
'firstname' => $newmembers_info['firstname'],
            
'lastname' => $newmembers_info['lastname'],
            
'status' => '1'
        
);

        
$this->db->set($insert_data);
        
$this->db->insert('members');

        
$this->db->where('member_id'$this->input->post('member_temp_id'));
        
$this->db->delete('members_temp');


        
$json['success'] = true;

    }

 
   $this->output->set_content_type('application/json');
 
   $this->output->set_output(json_encode($json));


You could default your status to 1 in your table and instead of building
a new insert_data array pass the database method the newmembers_info
array, it's already built.

That should help a little on the speed.
What about a separate nested AJAX for sending the email only?
Thanks all I have a idea or two now will let you all know later what I have done
(10-22-2017, 03:22 AM)InsiteFX Wrote: [ -> ]You could default your status to 1 in your table and instead of building
a new insert_data array pass the database method the newmembers_info
array, it's already built.

That should help a little on the speed.

I tried that now sill a few seconds delay.

PHP Code:
public function approved() {

$json = array();

if (
$this->input->post('approve')) {

 
  $getMemberInfo $this->get_member($this->input->post('member_id'));

 
  if ($getMemberInfo['member_id'] == $this->input->post('member_id') && $getMemberInfo['approved'] == 0) {

    
$updated $this->db->where('member_id'$this->input->post('member_id'))->update('members', array('approved' => '1''status' => '1'));

    
$json['success'] = true;

    if (
$updated) {

 
           $this->load->library('email');

     
   $this->email->from('*****''Admin');
     
   $this->email->to($getMemberInfo['email']);
     
   $this->email->subject('Riwaka Bowling Account Approved');
     
   $this->email->message('Hi, Just to confirm that your account has now been approved at Riwaka Bowling Club Admin Area');

     
   $this->email->send();
    }
 
   
}

$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($json));


Code:
$(document).ready(function() {
    $(document).on('click', '#button-approve', function() {
        $.ajax({
            url : "<?php echo base_url(); ?>admin/members/dashboard/approved",
            type : "POST",
            dataType : "json",
            data : {
                "approve" : true,
                "member_id" : $(this).attr('data-id')
            },
            success : function(data) {
                if (data['success'] = true)
                {
                    $('#new_member_approve').load(document.URL + ' #new_member_approve');
                }
            },
          
       });
    });

});
I use a mail queue for all my websites. It's really an essential thing. As long as you have access to cron:

1) Instead of sending the email, put it in a database table with one field "sent" set to 0;
2) Use cron to check for email that hasn't been sent, send it, then update the "sent" field to 1; (I have cron check for unsent email once per minute)

This type of behavior has a lot of advantages. For one, you can always check if mail is going out. Also, it's really easy to resend email just by marking "sent" as 0 again. There is 0 email sending lag time, because it's being put in a queue.
(10-22-2017, 11:02 PM)skunkbad Wrote: [ -> ]I use a mail queue for all my websites. It's really an essential thing. As long as you have access to cron:

1) Instead of sending the email, put it in a database table with one field "sent" set to 0;
2) Use cron to check for email that hasn't been sent, send it, then update the "sent" field to 1; (I have cron check for unsent email once per minute)

This type of behavior has a lot of advantages. For one, you can always check if mail is going out. Also, it's really easy to resend email just by marking "sent" as 0 again. There is 0 email sending lag time, because it's being put in a queue.

Not sure how to use cron at all.
You could try this also using pure PHP.

PHP Code:
// Instead of this
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($json));

// Try this it might speed it up pure PHP
header('Content-type: application/json');
echo 
json_encode($json); 
(10-23-2017, 08:52 AM)InsiteFX Wrote: [ -> ]You could try this also using pure PHP.

PHP Code:
// Instead of this
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($json));

// Try this it might speed it up pure PHP
header('Content-type: application/json');
echo 
json_encode($json); 

Its the email send() that delays it.
(10-22-2017, 11:28 PM)wolfgang1983 Wrote: [ -> ]
(10-22-2017, 11:02 PM)skunkbad Wrote: [ -> ]I use a mail queue for all my websites. It's really an essential thing. As long as you have access to cron:

1) Instead of sending the email, put it in a database table with one field "sent" set to 0;
2) Use cron to check for email that hasn't been sent, send it, then update the "sent" field to 1; (I have cron check for unsent email once per minute)

This type of behavior has a lot of advantages. For one, you can always check if mail is going out. Also, it's really easy to resend email just by marking "sent" as 0 again. There is 0 email sending lag time, because it's being put in a queue.

Not sure how to use cron at all.

Everyone has to learn sometime:
https://www.educba.com/linux-crontab/
Pages: 1 2