单击按钮后订阅系统不起作用

问题描述 投票:0回答:1

我有以下代码:

按钮:

<br>
<p>Programma's waarop u kunt abonneren:</p>
<form action="#" enctype="multipart/form-data" method="post" onSubmit="return processForm();">
    <?php wp_nonce_field( 'set_programma_action', 'set_programma' ); ?>
    <table>
        <?php foreach ( $retrieve_data as $retrieved_data ) { ?>
            <tr>
                <th>Programma:</th>
                <td id="hideit" style="vertical-align: middle;">
                <?php
                 echo $alreadysub; echo esc_html( $retrieved_data->Anaam );
                 ?>
                 </td>
                <th>
                <div><button id="some_id" style="display:block" onclick="hidenow('hideit')" name="programma" type="submit" value="<?php echo esc_attr( $retrieved_data->Anaam ); ?>">Abonneer</button></div>
                </th>
            </tr>
        <?php } ?>
    </table>
</form>

Javascript:

<script>
var divelement = document.getElementById("some_id");
window.addEventListener('DOMContentLoaded', function(){
    if(localStorage.getItem("form_submitted")){
        divelement.style.display = 'none';
    }else{
        //Local Storage Not Set
    }
});

function processForm(){
    //Send Ajax to submit data and after its response set the local Storage
    divelement.style.display = 'none';
    //Set
    localStorage.setItem("form_submitted", 1);
    return false; //this will prevent form from submitting and refreshing the page.
}

用户单击按钮后,他/她正在订阅程序。为了允许用户订阅程序,我创建了以下代码:

if (isset( $_POST['programma'] ) && isset( $_POST['set_programma'] ) && wp_verify_nonce( $_POST['set_programma'], 'set_programma_action' )) {

    $data = filter_input( INPUT_POST, 'programma', FILTER_SANITIZE_STRING );
    $current_user_id = get_current_user_id();

    if ( $current_user_id && ! empty( $data ) ) {
        //voeg de huidige user_id en data toe in de rij met meta_key programma
        $addprog = add_user_meta( $current_user_id, 'programma', $data );
        echo "U bent geabonneerd op". ' ' . $data;
    }
}
?>

我创建了Javascript代码,以确保用户订阅了程序后,他单击的按钮以及该程序就会消失。我唯一的问题是用户不再能够订阅程序。该程序也不会保存在数据库中。我已经搜索了几个有关此问题的youtube视频,但找不到解决方法。

此外,由于某些原因,单击时仅顶部按钮消失。不是臀部。

javascript php
1个回答
0
投票

如果要在提交表单时隐藏所有按钮,这是解决方案。为此,您必须使用class而不是id。

var divelement = document.getElementsByClassName("some_class");

function hideByClass(){
    for(var i = 0; i < divelement.length; i++){
                divelement[i].style.display = "none";
            }
}
window.addEventListener('DOMContentLoaded', function(){
    if(localStorage.getItem("form_submitted")){
         hideByClass();
    }else{
        //Local Storage Not Set
    }
});


function processForm(){
    //Send Ajax to submit data and after its response set the local Storage
    hideByClass();
    //Set
    localStorage.setItem("form_submitted", 1);
    return true;
}

并使用类代替id。您还必须在td上使用class,因为它也在循环中,因此id不应重复。

<div><button class="some_class" style="display:block" onclick="hidenow('hideit')" name="programma" type="submit" value="<?php echo esc_attr( $retrieved_data->Anaam ); ?>">Abonneer</button></div>
© www.soinside.com 2019 - 2024. All rights reserved.