user1688346
user1688346

Reputation: 1970

Ajax post not being hit

I am using codeigniter where i put the post backend coding under models/add_subscriber.php but the code not being hit. Any idea?

form.onsubmit = function(event) {
            validate();
            if (!isValid) {
                revalidateOnChange();
                return false;
            }
            var email = $("#signup_email").val();
            dataString = 'email=' + email;
            $.ajax(
                    {
                        type: "POST",
                        url: '<?php echo base_url()?>models/add_subscriber.php',
                        data: dataString,
                        success: function(response) {
                            alert(response);
                        },
                        error: function(xhr, textStatus, error) {

                        }
                    });
               return;

        };

Upvotes: 0

Views: 141

Answers (2)

giordanolima
giordanolima

Reputation: 1218

Create a controller in the "controllers" folder... Create a file something like this:

class your_controller extends CI_Controller {
    function example_function(){
        // PUT HERE YOU CODE that is in the models.addsubscriber.php
    }
}

In your code JS script, put this URL:

url: '<?php echo base_url()?>your_controller/example_function/'

Upvotes: 1

ARIF MAHMUD RANA
ARIF MAHMUD RANA

Reputation: 5166

try to figure out step by step,

  1. Your request is working check in firebug or chrome developer tool net panel may be javascript error there.
  2. hit directly the url see the url is valid view source of the javascript and copy the ajax url and paste it to address bar and see whether your url is valid.
  3. if url is ok and ajax is working and the response isn't 404 than check your controller, htaccess etc.

Suggestion

if you want a URL access to a resource (such as css, js, image), use base_url(), otherwise, site_url() is better. source

Upvotes: 1

Related Questions