如何提交值并显示添加/删除按钮?

时间:2019-07-09 05:58:04

标签: php wordpress

我有一个数组:

$userPosts

然后我有一个表单,并检查是否存在当前页面ID,如果显示,则显示“删除”按钮,否则显示“添加”按钮

<form id="savedPosts" action="" method="POST" class="" autocomplete="off">
    <input type="hidden" name="save_post" value="<?php echo $postId; ?>"> 
    if (in_array($postId, $userPosts)) {?>
            <button id="remove_post" name="remove_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box">Remove</button>
        <?php } else {   
        ?>
            <button id="save_post" name="save_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box">Add</button>
    <?php  }
</form>

然后我创建了发送位:

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if (isset($_POST['save_post'])) {
        $userPosts = implode(',', $userPosts);
        $userPosts = trim($userPosts);
        $userPosts = ltrim($userPosts, ",");    
        update_user_meta( $user_id, 'save_post', $userPosts );
    }
    if (isset($_POST['remove_post'])) {
        $userPosts = explode(',', $userPosts);
        if (($key = array_search($postId, $userPosts)) !== false) {
            unset($userPosts[$key]);
        }
        $userPosts = implode(',', $userPosts);
        $userPosts = trim($userPosts);
        $userPosts = ltrim($userPosts, ",");

        update_user_meta( $user_id, 'save_post', $userPosts );
    }
}

可以,但是当我单击addremove时,如果idarray中,则页面会发送正确的值,但是会显示旧按钮。我需要手动刷新以显示正确的按钮。

看起来像某种缓存。我尝试使用

ob_start();
$myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
header('Location: '.$myurl);
ob_end_flush(); 

但是我仍然看到旧的按钮,不确定是否按照以下说明将该位放置在错误的位置,或者我使用它的方式完全错误

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if (isset($_POST['save_post'])) {
        $userPosts = implode(',', $userPosts);
        $userPosts = trim($userPosts);
        $userPosts = ltrim($userPosts, ",");    
        update_user_meta( $user_id, 'save_post', $userPosts );
        ob_start();
        $myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        header('Location: '.$myurl);
        ob_end_flush();
    }
    if (isset($_POST['remove_post'])) {
        $userPosts = explode(',', $userPosts);
        if (($key = array_search($postId, $userPosts)) !== false) {
            unset($userPosts[$key]);
        }
        $userPosts = implode(',', $userPosts);
        $userPosts = trim($userPosts);
        $userPosts = ltrim($userPosts, ",");

        update_user_meta( $user_id, 'save_post', $userPosts );
        ob_start();
        $myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        header('Location: '.$myurl);
        ob_end_flush();
    }
}

完整代码

<?php 
    global $bodyClass;
    $bodyClass = "single";

/*
    Template Name: External
*/

    include ('header_single_b.php'); 
?>

<main role="main" class="flex-shrink-0">

    <?php
        $postId = $_GET['postId'];
        $allposts = '';
        $user_id = get_current_user_id();
        $userPosts= get_user_meta( $user_id, 'save_post', TRUE );
        $userPosts = explode(',', $userPosts);
        $userPosts = array_values(array_unique($userPosts));
        $response = wp_remote_get('https://example.com/wp-json/wp/v2/posts?include='.$postId);
        if ( is_wp_error( $response ) ) {
            return;
        }
        $posts = json_decode( wp_remote_retrieve_body( $response ) );
        if ( empty( $posts ) ) {
            return;
        }
        // If there are posts.
        if ( ! empty( $posts ) ) {
            // For each post.
            foreach ( $posts as $post ) {
                $allposts .= '<a href="' . esc_url( $post->link ) . '" target=\"_blank\">' . esc_html( $post->title->rendered ) . '</a>  ' . esc_html( $fordate ) . '<br />'.$post->content->rendered;
            ?>
            <div class="container margin-top-80 margin-bottom-80">
                <div class="row">
                    <div class="col-8">
                        <form id="savedPosts" action="" method="POST" class="" autocomplete="off">
                            <input type="hidden" name="save_post" value="<?php echo $postId; ?>"> 
                            <?php
                                if (in_array($postId, $userPosts)) {?>
                                        <button disabled="disabled" type="submit" class="save_post disabled btn btn-outline-dark" data-toggle="tooltip" data-placement="top" title="già nei tuoi favoriti">Già nella box</button>
                                        <button id="remove_post" name="remove_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box">Rimuovi dalla box</button>
                                    <?php } else {   
                                    ?>
                                        <button id="save_post" name="save_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box">Salva nella box</button>
                                <?php  }
                            ?>
                        </form>
                        <hr>
                        <h2><?php echo esc_html( $post->title->rendered ); ?></h2>
                        <p><?php echo $post->content->rendered; ?></p>
                    </div>
                </div>
            </div>
            <?php }
        }
        if (!in_array($postId, $userPosts)){
            array_push($userPosts,$postId);
        }   
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            if (isset($_POST['save_post'])) {
                $userPosts = implode(',', $userPosts);
                $userPosts = trim($userPosts);
                $userPosts = ltrim($userPosts, ",");    
                update_user_meta( $user_id, 'save_post', $userPosts );
            }
            if (isset($_POST['remove_post'])) {
                $userPosts = explode(',', $userPosts);
                if (($key = array_search($postId, $userPosts)) !== false) {
                    unset($userPosts[$key]);
                }
                $userPosts = implode(',', $userPosts);
                $userPosts = trim($userPosts);
                $userPosts = ltrim($userPosts, ",");
                update_user_meta( $user_id, 'save_post', $userPosts );
            }
        }
    ?>
</main>
<?php include ('footer_single_compare.php'); ?>

1 个答案:

答案 0 :(得分:0)

如果您不使用WordPress HOOKS并直接编写代码,则PHP代码将从上到下运行。

在您的情况下,您的PHP处理块位于HTML表单之后。 这就是为什么它必须显示旧数据。

要解决此问题,只需将该块移到顶部,就可以了。

例如,您可以将其放在行的前面

if ( ! empty( $posts ) ) 

应该看起来像这样

        if (!in_array($postId, $userPosts)){
            array_push($userPosts,$postId);
        }   
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            if (isset($_POST['save_post'])) {
                $userPosts = implode(',', $userPosts);
                $userPosts = trim($userPosts);
                $userPosts = ltrim($userPosts, ",");    
                update_user_meta( $user_id, 'save_post', $userPosts );
            }
            if (isset($_POST['remove_post'])) {
                $userPosts = explode(',', $userPosts);
                if (($key = array_search($postId, $userPosts)) !== false) {
                    unset($userPosts[$key]);
                }
                $userPosts = implode(',', $userPosts);
                $userPosts = trim($userPosts);
                $userPosts = ltrim($userPosts, ",");
                update_user_meta( $user_id, 'save_post', $userPosts );
            }
        }

if ( ! empty( $posts ) ) {