简体   繁体   English

将执行 PHP 的按钮转换为 jQuery/Ajax

[英]Converting a button that executes PHP to jQuery/Ajax

I'm trying to write a feature for my Fantasy Football league that allows players to trade with each other.我正在尝试为我的梦幻足球联赛编写一个功能,允许玩家相互交易。 Functionally it all works fine, but as I've coded it all in PHP I have an annoying problem where any time a button is pressed the page is effectively refreshed twice.从功能上讲,一切正常,但正如我在 PHP 中编写的所有代码一样,我遇到了一个烦人的问题,即每次按下按钮时,页面都会有效地刷新两次。 I've read that I can get around this with jQuery and Ajax but sadly I don't really have any experience with either.我读过我可以用 jQuery 和 Ajax 解决这个问题,但遗憾的是我对这两个都没有任何经验。

Here's a small section of the code that allows logged in users to withdraw a trade offer they have made:下面是一小部分代码,允许登录用户撤回他们提出的交易报价:

echo "<input type='submit' id='btn-danger' name='withdraw".$trade_id."' value='Withdraw'>";
    if($_POST && isset($_POST['withdraw'.$trade_id])) {
    $withdraw = $link->prepare("DELETE FROM trade_id_offers_out WHERE trade_id_offer_id = ".$trade_id);
    $withdraw->execute();
    }

This creates a "Withdraw" button for each trade offer they have sent out and has a unique name of "withdraw" plus whatever number the offer is in the SQL table.这会为他们发出的每个交易报价创建一个“撤回”按钮,并具有唯一的“撤回”名称以及该报价在 SQL 表中的任何编号。

As I say functionally it works perfectly fine.正如我在功能上所说,它工作得非常好。 But it refreshes the page twice and I'm wondering how I can take this code and turn it into something a little more practical?但它刷新了页面两次,我想知道如何获取这段代码并将其变成更实用的东西?

Many thanks for your time.非常感谢您的宝贵时间。

First you should make sure you have included jQuery into your page html before any other jQuery (there are plenty of tutorials out there).首先,您应该确保在任何其他 jQuery 之前将 jQuery 包含在您的页面 html 中(那里有很多教程)。

Second you need to give the submit button a class so you can select it using a jQuery selector.其次,您需要给提交按钮一个 class 以便您可以使用 jQuery 选择器 select 它。 Change the php code of the button to this:将按钮的 php 代码更改为:

echo "<input type='submit' id='btn-danger' class='withdrawTradeBtn' name='withdraw".$trade_id."' value='Withdraw'>";

Finally you would make a ajax post request to your url (same url as your page in this case).最后,您将对 url 发出 ajax 发布请求(在这种情况下,与您的页面相同的 url)。 The js would look something like this and would need to be placed before the end of the html body tag or after all your buttons are rendered: js 看起来像这样,需要放置在 html 正文标记的末尾之前或在所有按钮都呈现之后:

(Note: I have not tested this but it should be pretty close to what you are after) (注意:我尚未对此进行测试,但它应该与您所追求的非常接近)

<script>
    //we need to wait for the page to be loaded in the users browser
    $(document).ready(function(){
        //we are selecting all the buttons with the class withdrawTradeBtn
        //we are binding to the click event so whenever any of the buttons are pressed the code will be ran.
        $('.withdrawTradeBtn').on('click', function(e){
            //we need to prevent the button from actually reloading the page
            e.preventDefault();
            //now we need to make a ajax post request to the url
            $.post(
                '/your/url', //the url to make the post
                {$(this).attr('name'):'Withdraw'}, //the submitted data 
                function(data, status, jqXHR) {// success callback
                    //here is were you would delete the button or whatever
                }
            );
        });
    });
</script>

Most likely you would want to delete the trade entry from the html.您很可能希望从 html 中删除交易条目。 You would do that in the success callback of the ajax post.您可以在 ajax 帖子的成功回调中执行此操作。 I cant really add anything here because I don't know what your html structure looks like.我真的不能在这里添加任何东西,因为我不知道你的 html 结构是什么样的。

Hope this helps, let me know if you need any more help!希望这会有所帮助,如果您需要更多帮助,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM