dpxyfa4718 2014-04-27 08:16
浏览 45
已采纳

单击按钮调用PHP文件

I have two different buttons in my page. clicking on the first one must call a php file named derive_rules.php while cliking on the second one must call another file named derive_rules1.php using ajax. For same i tried following code. But it is not working,

<script>

    $(document).ready(function() {

        $(".deriver").click(function() {

            var val = $("#keyword").val();
            var agent = $(this).attr('rel');
            if(val == '') {$("#keyword").focus(); return;}
            else {

                $.ajax ({

                    url: 'derive_rules.php', 
                    data: 'kw='+val+'&agent='+agent, 
                    type: 'GET', 
                    dataType: 'json', 
                    cache: 'false', 
                    success: function(data) {

                        if(data.success == 'true') {

                            $("#results_holder").show();
                            $("#results").html(data.msg);
                            $("#keyword").val('');

                        }

                        else {

                            alert(data.msg);

                        }
                    }
                });
            }
        });
    });

</script>

and those are mu buttons

<td><b>+ New Rule</b></td>
            <td><input type = "text" name = "Keyword" id = "keyword" style = "width:300px" placeholder = "Keyword or Phrase"/></td>
            <td><input type = "button" value = "Verify" class = "Buttons deriver" rel = "<?php echo $_GET['id']; ?>"/></td>
            <td><input type = "button" value = "Add" class = "Buttons deriver" rel = "<?php echo $_GET['id']; ?>"/></td>

        </tr></table>

what changes should i apply on my code to make it work???? as i want

  • 写回答

2条回答 默认 最新

  • doulifang5554 2014-04-27 08:32
    关注

    Here is the quick solution.

    HTML

    <button class="same" data="derive_rules">Button One</button>
    <br/>
    <button class="same" data="derive_rules1">Button Two</button>
    

    jQuery

    $(".same").click(function(){
       var url = $(this).attr("data");
       //alert(url);
       $.ajax ({
          url: url+".php",//pass the url here or you can use whatever I used .php. And do the other stuff etc.
       });
    });
    

    For more go to my JSFILLDE

    If you need my help. Please find me any of social network by searching yeshansachithak.

    Here Is Your Code - As You Want - For more explain

    <table>
       <tr>
         <td><b>+ New Rule</b></td>
         <td><input type="text" name="Keyword" id="keyword" style="width:300px" placeholder = "Keyword or Phrase"/></td>
         <td><input type="button" data="your_file_name" value="Verify" class="Buttons deriver" rel="<?php echo $_GET['id']; ?>"/></td>
         <td><input type="button" data="your_file_name" value="Add" class="Buttons deriver" rel="<?php echo $_GET['id']; ?>"/></td>
       </tr>
    </table>
    

    Just pass another attribute in your <button data="your_file_name" ...... Then use your ajax call as like you did. Your button class is driver. Please see below.

    <script>
    
        $(document).ready(function() {
    
            $(".deriver").click(function() {
            //Edit by Yesh
            var url = $(this).attr("data");
            //To check
            alert(url);
    
                var val = $("#keyword").val();
                var agent = $(this).attr('rel');
                if(val == '') {$("#keyword").focus(); return;}
                else {
    
                    $.ajax ({
    
                        //url: 'derive_rules.php', //Edit by Yesh
                        url: url+'.php',//You can use whatever extension (.html ect..)
                        data: 'kw='+val+'&agent='+agent, 
                        type: 'GET', 
                        dataType: 'json', 
                        cache: 'false', 
                        success: function(data) {
    
                            if(data.success == 'true') {
    
                                $("#results_holder").show();
                                $("#results").html(data.msg);
                                $("#keyword").val('');
    
                            }
    
                            else {
    
                                alert(data.msg);
    
                            }
                        }
                    });
                }
            });
        });
    
    </script>
    

    Thanks dude. If you need my help. Please find me any of social network by searching yeshansachithak.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题