Use Ajax to call PHP function for HTML button Onclick

前端 未结 2 561
温柔的废话
温柔的废话 2020-12-22 05:55

I have a an HTML button where I want the onclick to call a PHP function. I need to use AJAX to do this but I am totally lost. There is more that occurs inside the php functi

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 06:29

    Using jQuery (untested):

    function PrintDist() {
        var submitButton = $(this).find('*:submit');
    
        $.post(this.action, $(this).serialize(), function() {
            alert('Form submitted!');
            submitButton.attr('disabled', 'false').attr('value', 'Enter');
        });
    
        submitButton.attr('disabled', 'true').attr('value', 'Posting...');
    
        return false;
    }
    

    Use a proper form as your HTML (for people without Javascript, and for the script to work nicely):

提交回复
热议问题