manix Posted July 9, 2011 Share Posted July 9, 2011 Hey, I just made my first attempt to use AJAX to post comments and return them immediately but I failed as expected. So I'm posting the codes if someone can tell me where I did wrong? <script language="javascript" type="text/javascript"> <!-- function ajaxFunction(){ var ajaxRequest; try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ var ajaxDisplay = document.getElementById('newcomment'); ajaxDisplay.innerHTML = ajaxRequest.responseText; document.addcomment.time.value = ajaxRequest.responseText; } } var comment = document.getElementById('comment').value; var id = document.getElementById('id').value; var queryString = "?id=" + id + "comment=" + comment; ajaxRequest.open("GET", "addcomment.php" + queryString, true); ajaxRequest.send(); } //--> </script> addcomment.php <?php $id = $_GET['id']; $comment = $_GET['comment']; $datetime = date('H:i:s ')." ".date('d / F / Y'); $id = mysql_real_escape_string($id); $comment = mysql_real_escape_string($comment); include 'condb.php'; mysql_query("INSERT INTO comments (commentid, comment, datetime) VALUES('$id', '$comment', '$datetime')") or die(mysql_error()); mysql_close(); $display_string = $comment; echo $display_string; ?> and here's the div in the main file: echo "<div id='newcomment'>Da</div>"; echo "<br><br><br><table><tr><td> <form id='b' name='addcomment'> <div align='center'>Cpodeli swoeTo mHeHue<br> <textarea id='comment' rows='5' cols='60' maxlength='255' onKeyPress='check_length(this.form)'; onKeyDown='check_length(this.form)'></textarea><br></div> <input value='$id' id='id' type='hidden'/> <input size='1' value='255' id='chars' readonly='readonly' style='float:left'/> <input id='dobavi' type='button' value='Dobawi Mnenie' onclick='ajaxFunction()' style='float:right'/> </form></td></tr></table>"; EDIT: I somehow managed to fix it... Didn't even understand what I did but it worked lol.. Quote Link to comment https://forums.phpfreaks.com/topic/241485-first-ajax-attempt-failed-ofc/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.