How to call a php script/function on a html button click

后端 未结 7 2033
南旧
南旧 2020-11-27 19:20

Before someone has a go at me or marks this down, I have looked all over the internet to find out how to do this (including the same question on stackoverflow). I\'m new, an

7条回答
  •  遥遥无期
    2020-11-27 19:45

    Of course AJAX is the solution,

    To perform an AJAX request (for easiness we can use jQuery library).

    Step1.

    Include jQuery library in your web page

    a. you can download jQuery library from jquery.com and keep it locally.

    b. or simply paste the following code,

    
    

    Step 2.

    Call a javascript function on button click

    
    

    Step 3.

    and finally the function

     function foo () {
          $.ajax({
            url:"test.php", //the page containing php script
            type: "POST", //request type
            success:function(result){
             alert(result);
           }
         });
     }
    

    it will make an AJAX request to test.php when ever you clicks the button and alert the response.

    For example your code in test.php is,

    
    

    then it will alert "hello" when ever you clicks the button.

提交回复
热议问题