Skip to main content

How to fetch data (messages) in every second with ajax (setinterval) from mysql (PHP) in wordpress?

Fetch data (messages) in every second with ajax (setinterval) from mysql (PHP) in wordpress.


In main file

<script>

jQuery(document).ready(function($){



var own_id = $("#own_id").val();
var user_id = $("#user_id").val();
alert('helo');


function sandeep(){

var data = {
'action':'fetch_message',
'own_id':own_id,
'user_id':user_id,
}


$.post(myajax.ajaxurl,data,function(response){
//$("#response").html(response);
$("#jassi").html(response);
console.log(response);
});


}


setInterval(function(){ sandeep(); }, 500);
});

</script>



<div id="jassi">



</div>


In Funtion.php file 




function fetch_message(){
$msg_content = $_POST['msg_content'];
$own_id = $_POST['own_id'];
$user_id = $_POST['user_id'];
echo $user_id;
global $wpdb;
$table_name13 = $wpdb->prefix . "user_chats";
$results = $wpdb->get_results("select * from $table_name13 where sender_id ='$own_id' AND receiver_id='$user_id'");
$total = count($results);

foreach($results as $result){
echo $result->msg_content."<br>";




}
die();
}

add_action('wp_ajax_nopriv_fetch_message','fetch_message');
add_action('wp_ajax_fetch_message','fetch_message');

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...