单击按钮调用PHP文件

时间:2014-04-27 08:16:14

标签: php jquery ajax

我的页面中有两个不同的按钮。单击第一个必须调用名为derive_rules.php的php文件,而第二个必须使用ajax调用另一个名为derive_rules1.php的文件。对于同样的我尝试下面的代码。但它没有用,

<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>

那些是mu按钮

<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>

我应该对我的代码应用哪些更改才能使其正常工作?因为我想要

2 个答案:

答案 0 :(得分:0)

查看按钮上的类,需要更改选择器

    $(document).ready(function() {
        $(".deriver0").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) {
                            alert(data);
                        }
                });
            }
        });
        $(".deriver1").click(function() {
            var val = $("#keyword").val();
            var agent = $(this).attr('rel');
            if(val == '') {$("#keyword").focus(); return;}
            else {
                $.ajax ({
                    url: 'derive_rules1.php', 
                    data: {kw:val,agent:agent},
                    type: 'GET', 
                    dataType: 'json', 
                    cache: 'false', 
                    success: function(data) {
                            alert(data);
                        }
                });
            }
        });
    });
</script>
<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 deriver0" rel = "<?php echo $_GET['id']; ?>"/></td>
            <td><input type = "button" value = "Add" class = "Buttons deriver deriver1" rel = "<?php echo $_GET['id']; ?>"/></td>

        </tr></table>

答案 1 :(得分:0)

这是快速解决方案。

<强> 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.
   });
});

有关更多信息,请转到我的JSFILLDE

如果你需要我的帮助。请通过搜索yeshansachithak找到我的社交网络。

以下是您的代码 - 如您所愿 - 有关详细说明

<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>

只需传递<button data="your_file_name" .....中的其他属性即可。然后像你一样使用你的ajax调用。您的按钮类为driver。请看下面。

<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>

谢谢老兄。 如果你需要我的帮助。请通过搜索yeshansachithak找到我的社交网络。