Title: refresh php function with jquery
Last modified: May 12, 2019

---

# refresh php function with jquery

 *  Resolved [decree](https://wordpress.org/support/users/decree/)
 * (@decree)
 * [7 years ago](https://wordpress.org/support/topic/refresh-php-function-with-jquery/)
 * hey everybody.
    i want to refresh a php function every 30 sec and my code is :—//
   php function
 *     ```
       <?php
       function circlemessageshow() {
         global $wpdb;
         $mymessage  = $wpdb->get_results("SELECT option_value FROM wp_options WHERE option_id = 142");
         foreach($mymessage as $page){
           $circlemessageshowbox = $page->option_value;
           return $circlemessageshowbox;
         }
       };
       ?>
       ```
   
 * save in refresh-circlemessage.php
 * // my js code
 *     ```
       function circlemessage() {
       	// I test every code But not Working // tested one by one
       	// return circlemessageshow() function => output : show content but not refresh data every 2sec
       return document.getElementById('circlemessageshow-refresh').innerHTML = refresh_object.php_function;
   
       	// when load refresh-circlemessage.php and load circlemessageshow() function in this file => output :  Call to a member function get_results() on null
       	$("#circlemessageshow-refresh").load(refresh_object.php_url);
   
       }
   
       $(document).ready(function() {
       	setInterval(circlemessage,2000);
       });
       ```
   
 * enqueue script file with localize
 *     ```
       function add_refreshjquery() {
         wp_register_script( 'refresh-js', get_template_directory_uri(). '/js/refresh.js', array('jquery'), '1.0.0', true );
         wp_localize_script( 'refresh-js','refresh_object',array(
           'php_url' => get_template_directory_uri(). '/template-parts/refresh-circlemessage.php',
           'php_function' => circlemessageshow(),
           'php_url_config' => get_site_url(). '/wp-config.php',
           'php_url_load' => get_site_url(). '/wp-load.php'
         ));
         wp_enqueue_script('refresh-js');
       }
       add_action('wp_enqueue_scripts', 'add_refreshjquery');
       ```
   
 * and html tag for show message
 *     ```
           <div class='circlemessage-esteam-modalbox-body' id='circlemessageshow-refresh-content'>
               <div id='circlemessageshow-refresh'></div>
   
             </div>
       ```
   
 * anybody can help me to resolve it ( if another way can work , plz say it)
    ty
   for every body Good Luck

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years ago](https://wordpress.org/support/topic/refresh-php-function-with-jquery/#post-11522559)
 * Your PHP assumes the WordPress environment is loaded, but your JS calls directly
   to a PHP file that is not loading WordPress.
    You don’t need all of WordPress
   to read the database, so you could do that without it, but make sure that you
   haven’t left any security holes or hackers will just use it to red the passwords
   of your users. To use WordPress from JS, you can use the WordPress AJAX interface
   or the REST API.
 *  Thread Starter [decree](https://wordpress.org/support/users/decree/)
 * (@decree)
 * [7 years ago](https://wordpress.org/support/topic/refresh-php-function-with-jquery/#post-11523158)
 * dear joy
    thank you for answer and for showing way to resolve this problem
 *  Thread Starter [decree](https://wordpress.org/support/users/decree/)
 * (@decree)
 * [7 years ago](https://wordpress.org/support/topic/refresh-php-function-with-jquery/#post-11537289)
 * hey guys
    i ask question a few days ago about refresh php function with jquery
   and my dear friend joy said : use wordpress ajax interface i solved my problem
   and share it : ::::::::::::: my js code :::::::::::::
 *     ```
       jQuery(document).ready(function($) {
       	var data = {
       		'action': 'refresh_message'     // We pass php values differently!
       	};
       	// We can also pass the url value separately from ajaxurl for front end AJAX implementations
       	function refreshmessage() {
   
       		jQuery.post(refresh_object.php_ajax, data, function(response) {
       			$("#circlemessageshow-refresh").html(response);
       		});
       	}
   
       setInterval(refreshmessage,000);
       });
   
       <a href="https://codex.wordpress.org/AJAX_in_Plugins" rel="noopener" target="_blank">source</a>
       ```
   
 * ::::::::::::: my php code :::::::::::::
 *     ```
       function add_refreshjquery() {
         wp_register_script( 'refresh-js', get_template_directory_uri(). '/js/refresh.js', array('jquery'), '1.0.0', true );
         wp_localize_script( 'refresh-js','refresh_object',array(
           'php_ajax' => admin_url( 'admin-ajax.php' ),
         ));
         wp_enqueue_script('refresh-js');
       }
       add_action('wp_enqueue_scripts', 'add_refreshjquery');
   
       add_action( 'wp_ajax_refresh_message', 'my_action' );
       function my_action() {
       	global $wpdb;
         $mymessage  = $wpdb->get_results("SELECT option_value FROM wp_options WHERE option_id = 142");
         foreach($mymessage as $page){
           $circlemessageshowbox = $page->option_value;
           echo $circlemessageshowbox;
         }
         wp_die();
       }
       ```
   
 * [source](https://codex.wordpress.org/AJAX_in_Plugins)
    -  This reply was modified 7 years ago by [decree](https://wordpress.org/support/users/decree/).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘refresh php function with jquery’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [decree](https://wordpress.org/support/users/decree/)
 * Last activity: [7 years ago](https://wordpress.org/support/topic/refresh-php-function-with-jquery/#post-11537289)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
