For some reason I can't get this to work. All I want to do is that when a button is clicked, (via ajax) a php file is executed. Aka, if I weren't using ajax, we'd be looking at:
出于某种原因,我不能让这个工作。我想要做的就是当点击一个按钮时,(通过ajax)执行一个php文件。 Aka,如果我没有使用ajax,我们会关注:
<a href="file.php">dfgdfg</a>
I want the file to be executed without leaving the page.
我希望在不离开页面的情况下执行文件。
This is what I've got atm:
这就是我所拥有的:
$(".vote-up").live('click', function(e) {
$.ajax("file.php", function(){
alert("Executed file");
});
});
That doesn't seem to be working. I'm just really confused how the jQuery ajax function functions in general, without dealing with anything remotely complicated.
这似乎不起作用。我真的很困惑jQuery ajax函数的功能如何,而不处理任何远程复杂的东西。
Added question:
补充问题:
.ajax({
url: 'includes/login-check-jquery.php',
success: function (data) {
if(data == 1){
alert("Logged in!!!");
}else{
window.location = data;
}
error: function (xhr, status, error) {
// executed if something went wrong during call
if (xhr.status > 0) alert('Error: ' + status); // status 0 - when load is interrupted
}
});
});
In the above code, if the data returned equals "logged in" than a message box appears. Otherwise it'll redirect to the location (that is sent in data). For some reason this if statement isn't working, but the data is being returned as expected.
在上面的代码中,如果返回的数据等于“登录”,则显示消息框。否则它将重定向到该位置(在数据中发送)。由于某种原因,此if语句不起作用,但数据按预期返回。
My PHP code is:
我的PHP代码是:
<?php
if (!$user->logged_in){
echo "../login/index.php";
}else{
echo "1";
}
?>
?>
Any ideas?
有任何想法吗?
1 个解决方案
#1
5
If you don't want to leave the page, do
如果您不想离开页面,请执行此操作
$(".vote-up").live('click', function(e) {
e.preventDefault();
...
And ajax could be updated, if desired to
如果需要,可以更新ajax
$.ajax({
url: 'file.php',
success: function (data) {
// this is executed when ajax call finished well
alert('content of the executed page: ' + data);
},
error: function (xhr, status, error) {
// executed if something went wrong during call
if (xhr.status > 0) alert('got error: ' + status); // status 0 - when load is interrupted
}
});
error callback part could be removed, if you go for simplicity and not usability and some more options could be added by referencing jquery ajax doc.
错误回调部分可以删除,如果你为了简单而不是可用性,可以通过引用jquery ajax doc添加更多选项。
#1
5
If you don't want to leave the page, do
如果您不想离开页面,请执行此操作
$(".vote-up").live('click', function(e) {
e.preventDefault();
...
And ajax could be updated, if desired to
如果需要,可以更新ajax
$.ajax({
url: 'file.php',
success: function (data) {
// this is executed when ajax call finished well
alert('content of the executed page: ' + data);
},
error: function (xhr, status, error) {
// executed if something went wrong during call
if (xhr.status > 0) alert('got error: ' + status); // status 0 - when load is interrupted
}
});
error callback part could be removed, if you go for simplicity and not usability and some more options could be added by referencing jquery ajax doc.
错误回调部分可以删除,如果你为了简单而不是可用性,可以通过引用jquery ajax doc添加更多选项。