andy_b_1502 Posted February 1, 2012 Share Posted February 1, 2012 Hi everyone! i'm new to Javascript and using Ajax. I've been blatent just to get my head round it personally. I've basically cut and pasted a script but it has syntax errors, whats going wrong here, where do i put: jQuery.ajax() // JavaScript Document $(document).ready(function() { // put all your jQuery goodness in here. //global vars var inputUser = $("#nick"); var inputMessage = $("#message"); var loading = $("#loading"); var messageList = $(".content > ul"); //check if all fields are filled function checkForm(){ if(inputUser.attr("value") && inputMessage.attr("value")) return true; else return false; } function updateShoutbox(){ //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); } }); } //Load for the first time the shoutbox data updateShoutbox(); //on submit event $("#form").submit(function(){ if(checkForm()){ var nick = inputUser.attr("value"); var message = inputMessage.attr("value"); //we deactivate submit button while sending $("#send").attr({ disabled:true, value:"Sending..." }); $("#send").blur(); //send the post to shoutbox.php $.ajax({ type: "POST", url: "shoutbox.php", data: "action=insert&nick=" + nick + "&message=" + message, complete: function(data){ messageList.html(data.responseText); updateShoutbox(); //reactivate the send button $("#send").attr({ disabled:false, value:"Shout it!" }); } }); } else alert("Please fill all fields!"); //we prevent the refresh of the page after submitting the form return false; }); }); The syntax error occurs on line 17? any help would be seen as a great help! many thanks Quote Link to comment https://forums.phpfreaks.com/topic/256208-newbie-help-needed/ Share on other sites More sharing options...
Andy-H Posted February 1, 2012 Share Posted February 1, 2012 I assume the error is if(inputUser.attr("value") && inputMessage.attr("value")) if ( inputUser.val().length && inputMessage.val().length ) In google chrome you can hold Ctrl + Shift and press J to get the javascript console, this will help you with js errors. Quote Link to comment https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1313437 Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 Andy-H is correct, I ran your code through JSLint (a rather handy tool to quickly review javascript code) and it outputtued this error: Problem at line 14 character 32: Unexpected '&'. if(inputUser.attr("value") &&amp... frankly, that snippet of code doesn't make sense. Also as noted, there are browser tools to help you debug your JavaScript code. Developer tools for chrome (as Andy-H noted), and the firefox add-on "firebug". Edit: also, Andy-H provided the correct syntax for the if statement. Quote Link to comment https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1313471 Share on other sites More sharing options...
andy_b_1502 Posted February 3, 2012 Author Share Posted February 3, 2012 Thanks guys! i'll bear the hotkeys in mind for checking and will get the tool too. It's up on the site now but the messages of the shoutbox do not seem to upload into it. Is there a specific thing i should be looking for to fault find, i have (from the guy's .zip file) a .js file and a .php file. When the send button is clicked nothing happens, and nothing gets sent to the table in my DB either?? from what the tutorial suggests; it should send the message to the table, retreive it and display them instantly. Quote Link to comment https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1314077 Share on other sites More sharing options...
andy_b_1502 Posted February 3, 2012 Author Share Posted February 3, 2012 this is what i have so far: http://www.360transport.co.uk/shoutbox.html as you will notice if you made a comment, it doesn't show automatically like it should, nothing happens on the page or any data goes into the mySQL table. What's happening here people? any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1314154 Share on other sites More sharing options...
andy_b_1502 Posted February 3, 2012 Author Share Posted February 3, 2012 --Update--- the server, DB name, password etc for mySQL is all correct, i'm guessing it could be more to do with the PHP side of things?? i'll shift this thread if somebody gives me the heads up, many thanks Quote Link to comment https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1314158 Share on other sites More sharing options...
Andy-H Posted February 3, 2012 Share Posted February 3, 2012 <script type="text/javascript" src="file:///C|/Users/Andrew/Desktop/Shoutbox/jquery.js"></script> <script type="text/javascript" src="file:///C|/Users/Andrew/Desktop/Shoutbox/shoutbox.js"></script> Quote Link to comment https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1314165 Share on other sites More sharing options...
andy_b_1502 Posted February 5, 2012 Author Share Posted February 5, 2012 i'm sorry i think i need a liitle more help, i'm a complete noob and i'm reading the books right now too lol. What are you suggesting here? Quote Link to comment https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1314704 Share on other sites More sharing options...
andy_b_1502 Posted February 5, 2012 Author Share Posted February 5, 2012 i changed it so it looks like this: <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="shoutbox.js"></script> but now i get the error messge: Could not connect: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 Quote Link to comment https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1314706 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.