0

I'm trying to process some PHP after a button is clicked with Jquery's AJAX function. The problem is that I can't link the JQuery's AJAX function with my PHP. There is no data to be send, I just want to procces some PHP code after clicking it. I tried adding a dummy data field in the AJAX code called 'btnpressed', but it didn't work.

I tried to find a solution, but, most sites just stick with form/data processing.

PS: note that the PHP code is on the same page as the HTML and Javascript.

HTML:

<button id='btn'>Do something</button>

Javascript/JQuery:

<script>
$(document).ready(function() {
   $("#btn").click(function() {
    $.ajax({
        type:"POST",
        url: "samepage.php"}
        });
    });

});
</script>

PHP:

if(isset($_POST['btnpressed'])){
//Do some database stuff
}
4

1 回答 1

0

JavaScript:

$(document).ready(function() {
   $("#btn").click(function() {
     $.ajax({
        type:"POST",
        url: "samepage.php",
        data: { btnpressed: 'test' },
        success: function(data) {
           console.log(data);
        }
     });
   });
});

PHP:

if (isset($_POST['btnpressed'])) {
   echo 'too localized';
}
于 2013-04-27T16:22:26.210 回答