AustinT
AustinT

Reputation: 2026

AJAX is not calling PHP?

Can anyone please point out exactly what I am doing wrong, I am trying to make a php call when a selected value is changed.

The code is not echoing the information from the php file.

JQUERY CODE

// Skill sort on change
$('#order_by').on('change', function() {
    $.ajax({
        type: "POST",
        url: "sort_skill_be.php",
        data: {skill:this.value}  
    }).done(function(result){
        console.log(result)
    })
});

PHP CODE

<?php
session_start();

$skill_sort = $_POST['skill'];
echo $skill_sort;
echo 'I got in here';


?>

Thank you for the help and time!

EDIT: It works correctly now, Thanks for all the help!

Upvotes: 3

Views: 4330

Answers (4)

Ilia Choly
Ilia Choly

Reputation: 18557

You should use then instead of done http://promises-aplus.github.io/promises-spec/

$('#order_by').on('change', function () {
    $.post("sort_skill_be.php", {
        skill: $(this).val()
    }).then(function (result) {
        console.log(result);
    }).fail(function () {
        console.err('failed to fetch data');
    });
});

Upvotes: 0

klewis
klewis

Reputation: 8340

You could test things out like this...

// Skill sort on change
$('#order_by').on('change', function() {
    $.ajax({
        type: "POST",
        url: "sort_skill_be.php",
        data: {skill:this.value}  
    }).done(function(result){
      console.log('my results' + results);
    })
});

Upvotes: -1

cssyphus
cssyphus

Reputation: 40030

Try this:

$('#order_by').on('change', function() {
    var sk = $(this).val();
    $.ajax({
        type: "POST",
        url: "sort_skill_be.php",
        data: 'skill=' + sk,
        success: function(result) {
            alert(result);
        }
    });
});

Upvotes: 3

Khawer Zeshan
Khawer Zeshan

Reputation: 9646

Try this

$('#order_by').on('change', function() {
    $.ajax({
        type: "POST",
        url: "sort_skill_be.php",
        data: {skill:$(this).val()},
        success: function(result){
            alert("done");
        },
        error: function(){
            alert("error");
        }
    });
});

Upvotes: 0

Related Questions