Home >Backend Development >PHP Tutorial >例子就是一个button按钮,click后执行一段php代码,而不是连接到php页面

例子就是一个button按钮,click后执行一段php代码,而不是连接到php页面

WBOY
WBOYOriginal
2016-06-13 10:16:16980browse

求一个例子就是一个button按钮,click后执行一段php代码,而不是连接到php页面
我看网上说是用ajax,但是我不会用,求一段jquery $.ajax()方法写的一个小例子

------解决方案--------------------
测试过了直接用,把文件也保存为utf-8格式的,中文也不会乱码,希望对你有用,jquery要包含,
例子中我包含到线上的http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js
,实际从官网下载

//a.html

HTML code
   <title> ajax 提交 </title>  <meta http-equiv="Content-Type" content="text/html" charset="utf-8">  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>   <input name="txt_name" id="txt_name" type="text" value=""><input name="txt_age" id="txt_age" type="text" value=""><input type="button" onclick="btnClick();" value="提交"><script type="text/javascript">         function btnClick(){        var txtName = $('#txt_name').val();        var txtAge = $('#txt_age').val();        dataVal={txt_name:txtName,txt_age:txtAge};        $.ajax({           type: "GET",           url: "a.php",           data:dataVal,          // data: "name=John&location=Boston",           success: function(msg){                alert(msg);                       }        }); }</script> <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn