Dustin Scott
Dustin Scott

Reputation: 67

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.

It would be very bad to make for each button a file with PHP.

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

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)){}

To do it like if(isset($_POST)){} it will cause some errors if I add if(isset($_POST)){} for another button

This is the HTML:

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

Upvotes: 1

Views: 1162

Answers (1)

Marc B
Marc B

Reputation: 360662

Why not include a "source" parameter? e.g.

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

and then you check if($_POST['source'] == 'from_button_A') and go from there.

Upvotes: 4

Related Questions