Use Ajax to call PHP function for HTML button Onclick

前端 未结 2 562
温柔的废话
温柔的废话 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):

    <form action="xxx" method="post" onsubmit="return PrintDist()">
        <input text="text" name="airport1" id="airport1" />
        <input text="text" name="airport2" id="airport2" />
        <input type="submit" value="Enter" />
    </form>
    
    0 讨论(0)
  • 2020-12-22 06:48

    Use any javascript library to make ajax calls to the php script on the server. For example:

    • jquery
    • prototype

    Or if you just want really simple stuff, take a look at the sajax ajax toolkit for php

    0 讨论(0)
提交回复
热议问题