-1

I have a counter that takes a sum from a database column and updates on every page load. I need to convert this to a live counter that updates without page load.

It's on a WordPress site, and I'm hoping not to have to create a separate page outside of WP in order to send the JSON array.

1) Is there any way to use the current page to submit/receive the POST data? 2) What's the best process for converting a simple 'SELECT * FROM' to something dynamic. Is ajax/jQuery the right choice?

Thanks!

4

1 に答える 1

0

以下を使用して、5 秒ごとに値を更新します。

window.setInterval(function(){
        $.getJSON('/getLatestCount.php', function(data) {
            $('#id_of_element_where_it_needs_to_be_put').html(data.count);
        });
}, 5000);

クエリにフォームのコンテンツが必要な場合は、次を使用します。

window.setInterval(function(){
        $.getJSON('/getLatestCount.php', $("#my_form_id").serialize(), function(data) {
            $('#id_of_element_where_it_needs_to_be_put').html(data.count);
        });
}, 5000);

これは POST ではなく GET を使用しますが、それは問題ではないと思います。http ファイルでは$_GET['my_field_name']、フォームの値にアクセスするために使用します。

jsonデータをphpで出力できますecho json_encode()

あなたの質問を見て、あなたがワードプレスに何かを求めていることを私はあなたがこれをすべて行う方法を知っているかどうか確信が持てないので、ポーリングとウェブソケットについての詳細には立ち入りません. これが最も簡単な方法です。

于 2013-04-27T01:54:52.963 に答える