使用Ajax调用PHP函数的HTML按钮的onClick [英] Use Ajax to call PHP function for HTML button Onclick

查看:569
本文介绍了使用Ajax调用PHP函数的HTML按钮的onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要的onclick调用PHP函数的HTML按钮。我需要使用AJAX来做到这一点,但我完全失去了。还有更多的是发生在PHP函数里面,但我没有看到,包括它的需要。下面是code迄今:

 < HTML>
< BODY>
< FORM>
    <输入文本=文本名称= airport1 ID = airport1 />
    <输入文本=文本名称= airport2 ID = airport2 />
    <输入类型=按钮值=输入的onclick = PrintDist()/>
< / FORM>
< / BODY>
< / HTML>

< PHP

    功能PrintDist(){
        $ icao1 = $ _REQUEST [airport1];
        $ icao2 = $ _REQUEST [airport2];
    }
?>
 

解决方案

使用jQuery(未经测试):

 函数PrintDist(){
    VAR提交按钮= $(本).find('*:提交);

    $。员额(this.action,$(本).serialize()函数(){
        警报(形式提交了!');
        。submitButton.attr('残疾','假')ATTR('值','输入');
    });

    submitButton.attr(禁用,真)ATTR('值','发布...')。

    返回false;
}
 

使用适当的形式,你的HTML(人没有JavaScript,并为剧本很好地工作):

 <形式的行动=XXX的方法=邮报的onsubmit =返回PrintDist()>
    <输入文本=文本名称=airport1ID =airport1/>
    <输入文本=文本名称=airport2ID =airport2/>
    <输入类型=提交值=输入/>
< /形式GT;
 

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 function, but I didn't see the need of including it. Below is code thus far:

<HTML>
<BODY>
<FORM>
    <input text="text" name=airport1 id=airport1 />
    <input text="text" name=airport2 id=airport2 />
    <input type="button" value="Enter" onclick=PrintDist()/>
</FORM>
</BODY>
</HTML>

<?php

    function PrintDist(){
        $icao1 = $_REQUEST["airport1"];
        $icao2 = $_REQUEST["airport2"];
    }
?>

解决方案

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>

这篇关于使用Ajax调用PHP函数的HTML按钮的onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆