php - How to update <div> in every second with jQuery AJAX? -


i'm trying create simple chat , need update chat div in every second. here's div:

<div class="left">         <p>             <?php                 //chat dialogini mb'dan o‘qib beradi                 $result = $mysqli->query("select                     users.nick,                     text.text                                         text                     inner join users on users.user_id = text.user_id                     order                     text.chat_id asc");                  //echo $result = myquery("select * text");                 while ($row = $result->fetch_assoc()) {                     echo "<span class='user'>" . $row["nick"] . "</span>" . ": " . $row["text"] . "<br />";                 }             ?>         </p>     </div> 

here's ajax code:

$( document ).ready(function() {         var auto_refresh =  function () {             $('.left p').load("add_text.php", function(){             settimeout(auto_refresh, 800);             }).fadein();         }         auto_refresh();     }); 

add_text.php redirects again index.php:

 <?php session_start(); require "php/config.php"; //ma'lumotlar bazasi bilan bog‘lanish: bool = con funksiya $mysqli = new mysqli($config['db_host'], $config['db_username'], $config['db_password'], $config['db']); if ($mysqli->connect_errno) {     echo "failed connect mysql: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; }  if(!empty($_post['chat-text']) || !ctype_space($_post['chat-text'])) {      $mysqli->query("insert `text` (`user_id`, `text`)                      values ('" . htmlspecialchars($_session['id']) . "', '"                      . htmlspecialchars($_post['chat-text']) . "')"); }      header("location: http://chat.uz/index.php");     exit  ?> 

but updating not working. help, please.

the way set not work.

if redirect work (i doubt it) , index.php main page contains header , footer, putting of contents in .left p html element lead extremely invalid html , multiple .left p elements add 1 after 800 miliseconds, 2 more after 1600 miliseconds, 4 more after 3200 miliseconds, etc. etc.

that blow-up server start making ajax requests, adding more elements, making more simultaneous ajax requests.

note not redirecting when use ajax, load whatever html gets returned ajax request in .left p.

you should return results query directly json or html , process these results in javascript (in case of json).

i replace header redirect @ end of php script like:

echo json_encode($mysqli->fetch_all()); exit; 

and change load method ajax method allows process results.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -