Mam problem z dodaniem odświeżania do shoutboxa. Wszystko działa dobrze skrypt pobiera dane i je wyświetla. Gdy dodamy posta, po dodaniu wywoływana jest fukcja updateShoutbox();, pobiera dane i wyswietla. Chciałem dodać przycisk [Odśwież] wywoływałem to tak:
$(document).ready(function() { $('#odswiez').click(function() { updateShoutbox(); }); });
a tutaj kod PHP i Javascript, oraz index.html (znajduja sie w jednym katalogu shoutbox i index.html jest includowany do pliku php top.php a ten do plikow takich jak index.php, galeria.php):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" href="../styl/s6.css" type="text/css" media="screen" /> </head> <body> <div class="shoutbox_main"> <div id="panel_shoutbox"> <div id="shoutbox" style="display: none;"> <form method="post" id="form"> <table> <tr> </tr> <tr> </tr> <tr> </tr> </table> </form> <div id="container"> <div class="content"> <ul> </ul> </div> </div> <div class="tab"> <div class="top"> <div id="shoutbox_toggle"> </div> </div> </div> </div> </div> </body> </html>
$(document).ready(function(){ var inputUser = $("#nick"); var inputMessage = $("#message"); var loading = $("#loading"); var messageList = $(".content > ul"); function updateShoutbox(){ messageList.hide(); loading.fadeIn(); $.ajax({ type: "POST", url: "shoutbox.php", cache: false, data: "action=update", complete: function(data){ loading.fadeOut(); messageList.html(data.responseText); messageList.fadeIn(2000); } }); } function checkForm(){ if(inputUser.attr("value") && inputMessage.attr("value")) return true; else return false; } updateShoutbox(); $("#form").submit(function(){ if(checkForm()){ var nick = inputUser.attr("value"); var message = inputMessage.attr("value"); $("#send").attr({ disabled:true, value:"Wysylanie..." }); $("#send").blur(); $.ajax({ type: "POST", url: "shoutbox.php", cache: false, data: "action=insert&nick=" + nick + "&message=" + message, complete: function(data){ messageList.html(data.responseText); updateShoutbox(); $("#send").attr({ disabled:false, value:"Wyraz To!" }); } }); } else alert("Wypelnij wszystkie pola!"); return false; }); }); $(document).ready(function() { $('#odswiez').click(function() { updateShoutbox(); }); });
<?php require_once "../engine.php"; if(!$_POST['action']){ } else { switch($_POST['action']){ case "update": $res = dbquery("SELECT date, user, message FROM shoutbox ORDER BY date DESC LIMIT 10"); $result .= "<div class='contentin'><span class='shoutbox_nick'><b>".$row['user']." </b></span> <span class=\"date\"> ".$row['date']."</span><br /><span class=\"message\">".$row['message']."</span> </div>"; } $(document).ready(function(){ $('div.contentin:even').css('background', '#780000'); }); </script>"; break; case "insert": break; } } ?>
ale po kliknieciu nic sie nie dzieje
na wstępie mówie że chciałem edytować post @up, ale się rozwaliło kodowanie więc piszę pod nim.
Sam rozwiązałem problem

$(document).ready(function() { $('#odswiez').click(function() { var loading = $("#loading"); var messageList = $(".content > ul"); //just for the fade effect messageList.hide(); loading.fadeIn(); //send the post to shoutbox.php $.ajax({ type: "POST", url: "shoutbox.php", data: "action=update", complete: function(data){ loading.fadeOut(); messageList.html(data.responseText); messageList.fadeIn(2000); } }); }); });
co mnie zdziwiło pomogło ponowne napisanie tego samego(jest 2 razy w dokumencie) z taką różnicą że bez
function updateShoutbox(){ .... }
i bez wywoływania jej
updateShputbox();