简体   繁体   English

如何向一个PHP文件发送多个AJAX请求?

[英]How to send more than one AJAX request to one PHP file?

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. 我试图将多个AJAX请求发送到一个PHP文件,因为我正在创建一个大项目,并且在索引页面中有许多具有不同功能的按钮。

It would be very bad to make for each button a file with PHP. 使用PHP为每个按钮创建一个文件将是非常糟糕的。

This is my AJAX code: 这是我的AJAX代码:

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

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)){} 在PHP文件中,我无法添加if(isset($_POST['check'])){} ,但是如果我这样写它, if(isset($_POST)){}

To do it like if(isset($_POST)){} it will cause some errors if I add if(isset($_POST)){} for another button if(isset($_POST)){}做,如果我为另一个按钮添加if(isset($_POST)){} ,则会导致一些错误

This is the HTML: 这是HTML:

<input type="checkbox" name="check" class="check" id="check" checked>

Why not include a "source" parameter? 为什么不包括“源”参数? eg 例如

data: {check: val, source: 'from_button_A' }

and then you check if($_POST['source'] == 'from_button_A') and go from there. 然后检查if($_POST['source'] == 'from_button_A')并从那里继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM