Home » Php » php – DOM doesn't rerender?

php – DOM doesn't rerender?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am communicating with facebook api for getting user pages, and once I do the whole process, my html does not rerender with available pages user selected. So once the oauth process is completed, I get the pages and call the function that renders HTML – however, html changes only on page refresh. This is my code:

function renderSocialPage($provider = null)
{
    if (!empty($provider)) {
        $tab = $provider;
    } else {
        $tab = 'facebook';
    }

    $pageData = get_option('page_data');
    echo '<h1>Pages</h1>';

    ?>
    <div class="social-connections-wrapper" data-nav-tabs="">
        <div class="nav-tab-wrapper">
            <a href="#facebook"
               class="nav-tab<?php if ($tab == 'facebook') { ?> nav-tab-active<?php } ?>">Facebook</a>
            <a href="#google" class="nav-tab<?php if ($tab == 'google') { ?> nav-tab-active<?php } ?>">Google My
                Business</a>
        </div>

        <div id="facebook" class="tab-content" style="display:<?php echo $tab == 'facebook' ? 'block' : 'none' ?>;">
            <?php
            if (!empty($pageData)) {
                ?>
                <p>Connected pages:</p>
                <ul>
                    <?php
                    foreach ($pageData as $data) {
                        ?>
                        <li> <?= $data['name'] ?></li>
                        <?php
                    }
                    ?>
                </ul>

                <a href="#" id="facebook-connect-btn">Reconnect to facebook.</a>
                <?php
            } else {
                ?>
                <p>Connect your social accounts.</p>

                <a href="#" id="facebook-connect-btn">Connect to facebook</a>
                <?php
            }
            ?>
        </div>
    </div>
    <?php
}

function gcConnectReviews()
{
    $provider     = $_POST['provider'];
    $access_token = $_POST['accToken'];

    if ($provider === 'facebook') {
        $response  = wp_remote_get('https://graph.facebook.com/v5.0/me/accounts?access_token='.$access_token);
        $data      = json_decode($response['body']);
        $page_data = $data->data;

        $fb_rev_data = [];

        foreach ($page_data as $data) {
            $fb_rev_data [] = [
                'id'           => $data->id,
                'name'         => $data->name,
                'access_token' => $data->access_token
            ];
        }

        add_option('page_data', '', '', 'yes');
        update_option('page_data', $fb_rev_data);
    } elseif ($provider === 'google') {
        // ... 
    }

    // so once I call this function, it should do render html with connected pages, 
    // but it does it only on refresh
    $this->renderSocialPage($provider);
}

Any advice on what could do the trick, or where I am wrong?

How to&Answers: