Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/256208-newbie-help-needed/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1313437
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1313471
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1314077
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1314154
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/256208-newbie-help-needed/#findComment-1314706
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.