简体   繁体   English

发送表单时更改按钮/图标

[英]Change the button/icon when the form is sent

Im trying to create a very simple HEART button plugin on Wordpress.我正在尝试在 Wordpress 上创建一个非常简单的 HEART 按钮插件。 It is one of my very first plugins.这是我的第一个插件之一。 What I am trying to do is when the button in the form is clicked, so the icon that is inside it, will be replaced/changed for another one.我想要做的是当表单中的按钮被单击时,因此其中的图标将被替换/更改为另一个。

Here is my code:这是我的代码:

function create_the_heart_button($content){
    global $wpdb;
    $table_name = $wpdb->prefix . 'hearts_table';
    if( is_singular() && in_the_loop() && is_main_query() ){
        $id = get_the_ID();
        $user_id = wp_get_current_user();

        $wpdb->get_results( "SELECT * FROM $table_name WHERE (owner_id = $user_id->ID AND post_id = $id)" );
        if($wpdb->num_rows == 0){
            return $content .
            // next: create a form and add style input type=submit? 
            
            "
            <form method=\"POST\" id=\"heart-btn-form\">   
            <input type=hidden name=heart-btn value=$id>         
            <button id=\"heart-btn\">&#10084;</button>                                  
            </form>";

        } else if(isset($_POST['heart-btn'])) {
        /*when the button is clicked so this happens:  &#128540;*/
        return $content .

        "
        <form id=\"heart-btn-clicked\">   
        <input type=hidden name=heart-btn-clicked value=$id>         
        <button id=\"heart-btn\">&#128540;</button>                                  
        </form>";
    } 
}
return $content;

} }

Right now, the emoji that appears when the form is not submitted yet is: ❤ and i would like it to be replaced for: 😜现在,当表单尚未提交时出现的表情符号是:❤,我希望将其替换为:😜

I tried using the onclick function before, but it doesn't really work, because the page needs to be updated (it has to send information to the database).我之前尝试过使用 onclick 函数,但它并没有真正起作用,因为页面需要更新(它必须将信息发送到数据库)。

The else if doesn't really work. else if 真的不起作用。 It doesn't happen anything when I click on the heart.当我点击心脏时,它不会发生任何事情。 It loads the page, but the heart is still there.它加载了页面,但心脏还在那里。

Any ideas or suggestions on how I could solve it?关于我如何解决它的任何想法或建议? Thanks谢谢

For future recording, I solved it like this:为了以后的录音,我是这样解决的:

    global $wpdb;
    $table_name = $wpdb->prefix . 'hearts_table';
    if( is_singular() && in_the_loop() && is_main_query() ){
        $id = get_the_ID();
        $user_id = wp_get_current_user();

        $wpdb->get_results( "SELECT * FROM $table_name WHERE (owner_id = $user_id->ID AND post_id = $id)" );
        if($wpdb->num_rows == 0){
            return $content .
            // next: create a form and add style input type=submit? &#10084;
                        
            "<form method=POST id=\"heart-btn-form\">   
            <input type=hidden name=heart-btn value=$id>                     
            <button id=\"heart-btn\">&#10084;</button>                                   
            </form>";


        }else {
            /*when the button is clicked so this happens:  &#128540;*/
            return $content .
            "<form method=POST id=\"heart-btn-clicked\">   
            <input type=hidden name=heart-btn-clicked value=$id>         
            <button id=\"heart-btn-clicked\">&#128540;</button>                                    
            </form>"; 
        }  
    }
    return $content;
}

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

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