Jump to content

Function is only working once..


reportingsjr

Recommended Posts

I have an AJAX chat code thats fully working, except one part.. I have a function to post but I can only use it once, then when you try to submit it again nothing happens. I dont know if buttons only work once or what but here is my code:

<html><head><script type="text/javascript" language="javascript" src="includes/ajax.js"></script><script type="text/javascript">setTimers();function post(){	name = document.getElementById("name");	post = document.getElementById("post");	if(name.value != ""){	if(post.value != ""){	request[r] = Ajax(r, "chat2.php", "POST", "name="+name.value+"&post="+post.value);	request[r].onreadystatechange = checkpost;	function checkpost(){ 	 if (AjaxHandle(request[r], r)) {    if (response[r] == "succesful") {    }    else{    	 alert("There was an error while you were trying to post, please try again.");     } 	 }	name.value = "";	post.value = "";	}	}else{ alert("Please fill in some text to post!"); }	}else{ alert("Please enter you name!"); }}</script></head><body><div style="width: 500px; height: 200px; overflow: auto; padding: 5px" id="chatbox">loading messages....(This may take up to 30 seconds)</div><br />Name:<input name="name" id="name" type="text"><br>Post:<input name="post" size="80" id="post" type="text"><br><button onclick="post()">post</button></body></html>

Now it all works fine, it post it and such. but you can only post once then you will have to refresh the page because the button onclick doesnt call the post function again.I really need this to work!! thank you.*edit* You can find the working example here

Link to comment
Share on other sites

okay, I found one error. After you post once, and try it again my javascript error thing says "post is not a function", do you guys think that it stops the javascript or something.

Link to comment
Share on other sites

  • 4 weeks later...

You have set the same name on a function, a variable and an element, which is a terribly bad thing. The post variable replaces the function because they both have the same name.If the code runs in IE, you'll have a third object with the same name, because IE treats all ids as a kind of global variables.Give all your elements, variables and functions unique names/ids to solve this.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...