jquery .get function sending request multiple times
I have the following code in my head area:
<script>
$(function() {
$("#idNameHere").click(function() {
var title = $("#threadTitle").val();
var category = $("#category").val();
var message = CKEDITOR.instances['message'].getData();
errors = new Array();
if (title == "")
errors.push("\n\t- You must enter a title.");
if (category == "")
errors.push("\n\t- You must select a category.");
if (message == "")
errors.push("\n\t- You must enter a message.");
if (errors.length > 0)
{
var errorString = "Please fix the following errors and try
again:";
for (var i = 0; i < errors.length; i++)
errorString = errorString + errors[i];
alert(errorString);
}
else
{
$.get("./ajax/createThread.php", { title: title,
category:category, message: message }, function(data) {
if (!isNaN(data))
window.location='./viewThread.php?tid='+data;
else
alert(data);
});
}
});
Sometimes (but not always) it sends the request multiple times - so I end
up with thread objects being inserted into my database in multiples.
From what I've read, it's because the click function is within the
$(function() {}); area - However, if i take the click function out of that
area, the click event isn't caught and the code isn't run at all.
How can I get the click function to register, and ONLY run once? });
No comments:
Post a Comment