单击按钮调用PHP文件

单击按钮调用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

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.

look at classes on button , need to change selector

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