Karuppiah RK
Karuppiah RK

Reputation: 3964

ajax url won't call php file

I am trying to update the user input ratings through ajax call. This one alert(performance_rating) returned the user input ratings properly. But, I have a problem with my url. it won't call user_ratings.php. I don't know why? I have tried alert function in user_ratings.php page. But, it won't alert.

I have the user_ratings.php file in siteurl.com/include/pages/user_ratings.php How do I call my php file properly?

ajax request

$(function () {
    $('#form').on('submit', function (e) {
      performance_rating = $('input:radio[name=rating]:checked').val();
      e.preventDefault();
      $.ajax({
        type: 'POST',
        url: 'user_ratings.php',
        data: {
            rating: performance_rating
        },
        success: function() {
            alert(performance_rating);
        }
      });
    });
});

Upvotes: 0

Views: 2641

Answers (3)

Mad Angle
Mad Angle

Reputation: 2330

If you are sending your ajax request from localhost to your domain, then you have to use full site url in your ajax call as follows

$(function () {
    $('#form').on('submit', function (e) {
        performance_rating = $('input:radio[name=rating]:checked').val();
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'http://domain.com/include/pages/user_ratings.php',
            data: {
                rating: performance_rating
            },
            success: function() {
                alert(performance_rating);
            }
        });
    });
});

Upvotes: 1

crmpicco
crmpicco

Reputation: 17181

Put your FQDN (e.g. www.yoururl.com) before the user_ratings.php in your jQuery Ajax call. So change to this:

$(function () {
    $('#form').on('submit', function (e) {
      performance_rating = $('input:radio[name=rating]:checked').val();
      e.preventDefault();
      $.ajax({
        type: 'POST',
        url: 'http://www.yoururl.com/user_ratings.php',
        data: {
            rating: performance_rating
        },
        success: function() {
            alert(performance_rating);
        }
      });
    });
});

Upvotes: 0

Saswat
Saswat

Reputation: 12836

first its better you do this

define("URL", "http://siteurl.com/");

Then in the ajax url section write

url: '<?php echo URL ;?>includes/pages/user_ratings.php',

Upvotes: 0

Related Questions