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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?