Welcome Guest ( Log In | Click here to Register a free account now! )
Welcome to Bleeping Computer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.![]() ![]() |
Jun 1 2009, 10:12 AM
Post
#1
|
|
|
Senior Member ![]() ![]() ![]() ![]() Group: HJT Sophomore Classmen Posts: 434 Joined: 26-August 08 From: Victoria Member No.: 233,642 |
I know it's a big lump of code... sorry! It all works fine if i take the Ajax out and chuck in some php to grab the messages but i want it to auto update... This has got me stumped... EDIT: The original code i got from here http://woork.blogspot.com/2009/05/how-to-i...l-facebook.html (the site is a bit dodgy sometimes won't load). The code i have is: index.php CODE <html> <head> <title>B-rad's Wall</title> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache" /> <link rel="stylesheet" type="text/css" href="css.css" /> <script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("form#submit_wall").submit(function() { var message_wall = $('#message_wall').attr('value'); $.ajax({ type: "POST", url: "insert.php", data: "message_wall="+ message_wall, success: function(){ $("ul#wall").prepend("<li style='display:none'>"+message_wall+"</li>"); $("ul#wall li:first").fadeIn(); } }); return false; }); }); </script> </head> <body> <h2>B-rad's Wall</h2> <form action="" id="submit_wall"> <label for="message_wall">Your message: </label> <input type="text" id="message_wall" class="input" value="Message - First Name" /> <button type="submit">Write!</button> <button type="reset" onclick="window.location='http://202.45.110.174/social/test'">Refresh</button> </form> <table align="center" border="0" height="100%" width="100%"> <tr> <td width="32%"> </td> <td width="53%"> <!--Ajax Start--> <script type="text/javascript"> var xmlhttp; function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("_ticker").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } var init_ticker = function(){ xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="data.php"; xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); xmlhttp.timeout = 10000 }; setInterval(init_ticker, 1000); window.onload = init_ticker; </script> <div class="_ticker" id="_ticker">Loading...</div> <!--Ajax finish--> </td> <td width="15%"> </td> </tr> </table> <br /> <br /> <a href="http://www.redhost.com.au"><img src="redhost.jpg" align="center" border="0" /></a> <h4>Affordable Web Hosting and Design</h4> <h4>Email: Brad@redhost.com.au .:. <a href="http://www.redhost.com.au">www.Redhost.com.au</a></h4> </body> </html> data.php CODE <? $db_host="localhost"; $db_name="messages"; $username="username"; $password="*******"; $db_con=mysql_connect($db_host,$username,$password); $connection_string=mysql_select_db($db_name); mysql_connect($db_host,$username,$password); mysql_select_db($db_name); $result = mysql_query("select * from messages ORDER BY ID desc"); if(!$result) echo mysql_error(); else { while($row = mysql_fetch_row($result)) { ?> <ul id="wall"> <li> <? echo "$row[1]<br />\n"; ?> </li> </ul> <? } } ?> and the two javascripts up in the index.php page use this to insert the message into the database insert.php CODE <?php
if(isset($_POST['message_wall'])){ /* Connection to Database */ include('config.php'); /* Remove HTML tag to prevent query injection */ $message = strip_tags($_POST['message_wall']); $sql = 'INSERT INTO messages (messages) VALUES( "'.$message.'")'; mysql_query($sql); echo $message; } else { echo '0'; } ?> This post has been edited by KamakaZ: Jun 1 2009, 10:15 AM -------------------- If I am helping you and don't reply in 24 hours please send me a PM
Currently in training... There's no place like 127.0.0.1 There are 10 types of people in the world, those that can read binary, and those who can't. |
|
|
|
Jun 1 2009, 11:56 AM
Post
#2
|
|
![]() Hail Groovicus! ![]() ![]() ![]() ![]() ![]() ![]() Group: Site Admin Posts: 7,900 Joined: 5-June 04 From: Centerville, SD Member No.: 689 |
Sort of hard to say. That is where debugging comes in.
-------------------- |
|
|
|
Jun 2 2009, 03:56 AM
Post
#3
|
|
|
Senior Member ![]() ![]() ![]() ![]() Group: HJT Sophomore Classmen Posts: 434 Joined: 26-August 08 From: Victoria Member No.: 233,642 |
I have tried various things, such as removing doctype and other snippets of code... where would you start?
-------------------- If I am helping you and don't reply in 24 hours please send me a PM
Currently in training... There's no place like 127.0.0.1 There are 10 types of people in the world, those that can read binary, and those who can't. |
|
|
|
Jun 2 2009, 09:49 AM
Post
#4
|
|
![]() Hail Groovicus! ![]() ![]() ![]() ![]() ![]() ![]() Group: Site Admin Posts: 7,900 Joined: 5-June 04 From: Centerville, SD Member No.: 689 |
With a debugger and stepping through code. Firebug, whatever.
Yahoo has an AJAX library and tutorials that are much easier to handle than what you are doing. They take care of all of the browser inconsistencies, plus the tutorial is really good. I am using it for my AJAX requests, and once you understand what is happening, it is dead easy to use: http://developer.yahoo.com/yui/connection/ Here is my generic AJAX call from the project that I am working on: CODE function ajaxCall(url,obj, handleSuccess){ var handleFailure = function(o){ obj.innerHTML = 'Ajax load error' + url + '\n' + o; }; var callback = { success: handleSuccess, failure: handleFailure }; YAHOO.util.Connect.asyncRequest('GET', url, callback); } It gets three parameters; the type of request (which is a GET, the url, and a callback object). The callback object is already defined. If you notice in the parameters being passed to the ajaxCall function, one of them is an obj, and the other is a handleSuccess. This is where your function that uses ajaxCall tells what to do with the returned object. For instance, here is how I am getting objects to populate a menu: CODE function loadMenu(){ var url = urlBase + "extractsUtility?switch=0"; var obj = document.getElementById("menuContainer"); var handleSuccess=function(o){ try{ var menu = YAHOO.lang.JSON.parse(o.responseText); buildExtractMenu(menu); }catch(e){ obj.innerHTML = 'Failure processing menu items ' + url + '\n\n' + e; } }; ajaxCall(url,obj, handleSuccess); } You can see in the middle there that there is an inner function called handleSuccess (which gets passed to the ajaxCall function). What it is saying is that once I get my object back, then I pass it to another function that actually builds the menu. This is just one way of doing things. I could actually have the handleSuccess function inside the AJAX call if I was only using it for one thing, but since I am using it to handle multiple requests, it makes sense to make the AJAX call as generic as possible. See where that gets you. What I would recommend doing is starting a test project to work through the tutorial, and see what you can figure out. -------------------- |
|
|
|
Jun 3 2009, 07:26 PM
Post
#5
|
|
|
Senior Member ![]() ![]() ![]() ![]() Group: HJT Sophomore Classmen Posts: 434 Joined: 26-August 08 From: Victoria Member No.: 233,642 |
wow a generic AJAX call would be great! i'll have a look through it and also look into firebug or something similar... i'll keep you posted!
-------------------- If I am helping you and don't reply in 24 hours please send me a PM
Currently in training... There's no place like 127.0.0.1 There are 10 types of people in the world, those that can read binary, and those who can't. |
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 8th November 2009 - 03:03 AM |