如何向一个PHP文件发送多个AJAX请求? [英] How to send more than one AJAX request to one PHP file?
问题描述
我正在尝试将多个AJAX请求发送到一个PHP文件,因为我正在创建一个大项目,并且在索引页面中有很多具有不同功能的按钮。
I am trying to send multiple AJAX requests to one PHP file, because I am creating a big project and in the index page there is a lot of buttons which have different functionality.
用PHP为每个按钮创建一个文件是非常糟糕的。
It would be very bad to make for each button a file with PHP.
这是我的AJAX代码:
This is my AJAX code:
$('#check').click(function(){
var val = $(this).filter('input:checked').val();
$.ajax({
url: './process/upload.php',
type: 'POST',
data: {'check': val},
success: function(data){
alert(data);
}
});
});
在PHP文件中我无法添加 if(isset($ _ POST) ['check'])){}
它不起作用但如果我这样写 if(isset($ _ POST)){}
In the PHP file I can't add if(isset($_POST['check'])){}
It won't work but if I write it like this if(isset($_POST)){}
要像那样执行if(isset($ _ POST)){}
如果我这样做会导致一些错误添加 if(isset($ _ POST)){}
获取另一个按钮
To do it like if(isset($_POST)){}
it will cause some errors if I add if(isset($_POST)){}
for another button
这是HTML:
<input type="checkbox" name="check" class="check" id="check" checked>
推荐答案
为什么不包含source参数?例如
Why not include a "source" parameter? e.g.
data: {check: val, source: 'from_button_A' }
然后你检查 if($ _ POST ['source'] =='from_button_A')
和从那里开始。
and then you check if($_POST['source'] == 'from_button_A')
and go from there.
这篇关于如何向一个PHP文件发送多个AJAX请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!