0

I have a jQuery Ajax Post request which goes into a Code Igniter function, after which CodeIgniter sends an email with details in the post request.

JavaScript Code:

$.ajax({
    type: 'POST',
    url: 'http://localhost/admin/index.php/callback',
    data: { 'name': inputName, 'phone': inputNumber},
    success: function() {
        onSuccess();
},
async:true
});

PHP/CodeIgniter function

public function email() {
    if (!isset($_POST)) {
         //exit('No direct script access allowed');
    } else {
    //Send Email
    }
}

The Problem SEE EDIT

CodeIgniter does not send a response to the call.

The Post request is recieved and run as the email part works fine, I recieve the email with the details sent in the post request but onSuccess(); is never called (and no response is logged via Chromes network watcher, it just says 'Pending')

This is all ran off my local machine

This request is done from the main index.php BUT CodeIgniter is only installed in the /admin directory

Thanks for your time!

EDIT: Changed the ajax url to the full url with localhost as that's how it's set in CI $config

EDIT: CI does send a response, it takes 2-3 minutes which is also how long it takes the email to be sent, in that time I cannot access any other pages of the site. I am starting to think this may be a problem with my local machine/Apache setup.

5
  • Anything in the PHP error log? The PHP could be exceeding the max time limit and thus producing a 500 internal server error. Also try on Firebug to see if it hangs with no response like Chrome. Commented Jan 16, 2013 at 16:42
  • Ah! I think I found the problem, sendmail fails to send the email, retrys after 1 minute, fails again then retrys again and it works on the third try. I guess I have to sort out CIs email sending/sendmail. Commented Jan 16, 2013 at 16:59
  • Also declare the datatype on the ajax request: JSON, text or something else. Commented Jan 16, 2013 at 18:12
  • can you add error: to your ajax request and pop up an alert. This will prove that the problem is the controller and not onSuccess(); Commented Jan 16, 2013 at 18:18
  • No as I'm stuck with using jQuery 1.4 (I have tested with 1.9 and I had the same issue) the hang comes from PHP waiting for sendmail to send an email. Commented Jan 17, 2013 at 9:49

4 Answers 4

1
var base_url = '<?php echo base_url();?>';
$.ajax({
    type: "POST",
    url: base_url+"index.php/my_class/email",
    data: { name: inputName, phone: inputNumber},
    success: function() {
        // success actions....
    }
});

Controller

class My_class extends CI_Controller{
    public function email() {
        $name = $this->input->post('name');
        if (!$name) {
        //exit('No direct script access allowed');
        } else {
        //Send Email
        }
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

It is the exact same as the base url already (edited my post to note)
After your edit: My CI Class is constructed correctly, I just ommited everything but the function, everything is called, it just seems to hang for a few minutes when processing the email and not send a response.
0

Try the following code:

var url = '<?php echo base_url();?>index.php/my_class/email';

$.post(url, {
    data: { name: inputName, phone: inputNumber},
},
function(data_returned){
    alert(data_returned);
});

1 Comment

I was using post first, there was no change
0

You should you empty instead of !isset, because isset($_POST) is always TRUE, I think so:

public function email() {
    if (empty($_POST)) {
         //exit('No direct script access allowed');
    } else {
    //Send Email
    }
}

Comments

0

The problem was with my sendmail settings and PHP hanging/retrying until it got a response from sendmail.

2 Comments

@Sam...I am also facing the same problem..can you tell me what changes you have done??
It was to do with my fqdn settings as far as I can remember

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.