2013-06-20 48 views
4

任何人都可以请指出我到底做了什么错误,我想在选定的值更改时进行PHP调用。AJAX不调用PHP?

该代码没有回显来自php文件的信息。

jQuery代码

// 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代码

<?php 
session_start(); 

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


?> 

感谢您的帮助和时间!

编辑︰它现在正常工作,感谢所有的帮助!

+1

您是否使用过任何调试程序?萤火虫? Chromes开发人员工具?诊断任何Javascript/jquery错误? –

+0

你的'.done'函数没有任何内容,所以即使你的脚本完美运行,你也不会看到它做任何事情。在'done'处理程序中尝试'console.log(result)'。 –

+0

给出的代码很好。问题在于你没有引用的东西。 –

回答

4

试试这个:

$('#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); 
     } 
    }); 
}); 
+0

是的,它的工作,我的错!谢谢您的帮助! – AustinT

+0

'this.value'更好。 – hjpotter92

+0

我会尽可能接受答案。 – AustinT

1

试试这个

$('#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"); 
     } 
    }); 
}); 
0

你可以测试出来的东西像这样...

// 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); 
    }) 
}); 
1

您应该使用then代替donehttp://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'); 
    }); 
});