I make online consultant for chat. Messages from the user every 2 seconds are loaded via AJAX (I don’t need to shout that they don’t do this anymore, I know). So, I use the script:
function show() { $.ajax({ url: "<?bloginfo('template_url');?>/load_messes.php", cache: false, success: function(html){ $("#result_div_id").html(html); $i++; } }); } $(document).ready(function(){ show(); setInterval('show()',2000); }); And here is the message update code (load_messes.php):
<?php @ini_set('display_errors','Off'); @ini_set('error_reporting',0); define('WP_USE_THEMES', false); require( $_SERVER['DOCUMENT_ROOT'] .'/wp-blog-header.php'); global $wpdb; ?> <?php $_SESSION['session'] = $_SERVER["REMOTE_ADDR"]; $_SESSION['session'] .= session_id(); $session = $_SESSION['session']; $messages = $wpdb->get_results(' SELECT * FROM euro_messages WHERE SESSION = "'.$session.'" '); $seen = $wpdb->get_var(' SELECT id FROM euro_messages WHERE SESSION = "'.$session.'" and seen = 0 '); $seen_2 = $wpdb->get_var(' SELECT id FROM euro_messages WHERE SESSION = "'.$session.'" and seen = 0 and fromto > 0 '); $templatepath = get_bloginfo('template_url'); if($seen){ echo '<script> $(function () { setTimeout(function () { var $container = $(\'.container\'); $container[0].scrollTop = $container[0].scrollHeight; }, 100); }); </script>'; } if($seen_2){ echo ' <script> setTimeout(function soundClick() { var audio = new Audio(); // Создаём новый элемент Audio audio.src = \''.$templatepath.'/sounds/sms.mp3\'; // Указываем путь к звуку "клика" audio.autoplay = true; // Автоматически запускаем }, 100); </script> '; } for ( $d = 0; $d < count($messages); $d++) { $mess = $messages[$d]->message; $fromto = $messages[$d]->fromto; if($fromto == 0){ $mess_text = "<h4 style='margin:0;padding:0;'>Вы:</h4><font size=2>$mess</font><hr>"; }else{ $mess_text = "<h4 style='margin:0;padding:0;'>Консультант:</h4><font size=2>$mess</font><hr>"; } echo $mess_text; $wpdb->update( 'euro_messages', array( 'seen' => 1 ), array( 'session' => $session ) ); } if(!$messages){ echo '<font size=2 style="text-align: left;"> <i> Для начала диалога отправьте сообщение. </i> </font> '; } ?> My site is on the VP. When I send the message, it goes in essence, the same script, only there is loading another file - form.php. The message of the user is transferred to it and it is added to the database. But the output of load_messes.php for some reason works only in Chrome. Thank you in advance)
$isomewhere declared and initialized, everything should work. Addconsol.log($i + ": " + html);insuccess. - Igor<script></script>. Most likely not the correct url.url: "<?php echo bloginfo('template_url'); ?>/load_messes.php",orurl: "<?=bloginfo('template_url');?>/load_messes.php",- Mr. Black