Sunday 15 April 2012

php - Ajax Displaying Message -


This is a great piece of code, it's working very well, but the problem is that I'm always the same Receive new DIV with the message, how to change it, so it will appear in one place? The Msgsrv.php file is just a echo "test message"; but it will pull data from MySQL DB. So what I get is a test message and then in the next line of test message, I have to change the old message to the new message.

  & lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; Auction & lt; / Title & gt; & Lt; Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type = "text / javascript" charset = "UTF-8" & gt; & Lt; / Script & gt; & Lt; Style type = "text / css" media = "screen" & gt; Body {background: # 000; Color: #fff; Font-size: .9em; } .msg {background: #aaa; Padding: .2em; Border-bottom: 1px # 000 solid}. .old {background-color: # 246499;} New {background-color: # 3B9957;} .error {background-color: # 992E36;} & lt; / Style & gt; & Lt; Script type = "text / javascript" charset = "UTF-8" & gt; Function ADMSG (Type, msg) {/ * Simple Assistant is the name of a CSS class (type of old / new / error) is the content of Msg Div * / $ ("# message") to add a div. Append ("& lt; div class = 'msg" + type + "'>" + msg + ""); } Function waitForMsg () {/ * This URL "msgsrv.php" requests when it is full (or errors) * / $ Ajax ({type: "GET", url: "msgsrv.php"), async: true, / * if set to non-async, the browser shows the page as "Loading .." is. * / Cache: incorrect, timeout: 50000, / * MS * / timeout in success: function (data) {/ barge.php integer when requested; / ADMSG ("new", data); / * .msg DIVA (with the "new" class) * / setTimeout (waitforgum, / * request next message * / 1000 / *. Add after 1 second * /);}, error: function (XMLHttpRequest, textStatus, ErrorThrown) {AMSMG ("Error", TextStatus + "(" + Error Truro + ")"); Settimeout (Waiting Forms, / * Try Again .. * / 15000); / * Milliseconds (15seconds) *}}); }; $ (Document) .ready (function () {waitForMsg (); / * initial request * /}); & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Div id = "message" & gt; & Lt; Div class = "msg old" & gt; The last bid is: & lt; / Div & gt; & Lt; / Div & gt; & Lt; / Body & gt; & Lt; / Html & gt; As it says, "append" will add content to the Atom, while  

Html () will replace the whole internal HTML element and replace it with the content you set.

Change

  $ ("# messages"). Append ("& lt; div class = 'Msg' + type +" '> "+ msg +"  ");   

With:

  $ ("#moded") .html ("& lt; div class = 'msg" + type + "'> gt; + + + + + + & lt; / div & gt; ; ");    

No comments:

Post a Comment