douzhigan1687 2016-12-19 13:40 采纳率: 0%
浏览 118
已采纳

使用带有参数/ AJAX的按钮onClick调用PHP函数?

Hello I want to call my function download when the user click on the button, basically:

<input type='button' name='Release' onclick="document.write('<?php downloadFichier($tab1, $t2) ?>');" value='Click to Release'>

Of course doesn't work, so I try to do with a AJAX, I don't know this language, but it is possible to do what I want: call my PHP function with 2 parameters?

<button type="button">Click Me</button>
<p></p>
<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){

            $.ajax({
                type: 'POST',
                url: 'file_product.php',
                success: function(data) {
                    $("p").text(data);
                }
            });
   });
});
</script>

Thank you for helping me.

  • 写回答

2条回答 默认 最新

  • dongmu7335 2016-12-19 13:56
    关注

    You should call a js-function with onclick event like this:

    <input type='button' name='Release' onclick="downloadFichier(param1, param2)" value='Click to Release'>
    

    And your AJAX-function should be like this:

    function downloadFichier(param1, param2){
    
            $.ajax({
                type: 'POST',
                url: 'file_product.php',
                data: "param1=" + param1 + "&param2=" + param2,
                success: function(data) {
                    $("p").text(data);
                }
            });
    

    You can get your params in PHP-script from the $_REQUEST array by their names (param1, param2).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式