homepage Welcome to WebmasterWorld Guest from 54.226.0.225
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
jquery dialog post being duplicated
ploppy




msg:4343532
 11:50 am on Jul 25, 2011 (gmt 0)

Ths first time i make a post, the post submits fine. I then close the dialog window and click link to reopen and when i submit data again, it duplicates it and adds it twice. This behaviour just seems to keep adding a duplidcte each time. for example, post1 then post1+post1 then post1+post1+post1. Hope you get the idea. If someone could check my code, i would be grateful. Many thanks

// Feedback form

function feedbacknew() {

$("#fb_form").dialog({
autoOpen: false,
resizable: true,
modal: true,
title: 'Submit a feedback request',
width: 480,
beforeclose: function (event, ui) {
$("#fb_message").html("");


},
close: function (event, ui) {
$("#fb_message").html("");
$("#feedback").get(0).reset();
}

});

$('#fb_submit').click(function () {

var name = $('#fb_uname').val();
var client = $('#fb_client').val();
var department = $('#fb_department').val();
var email = $('#fb_email').val();
var position = $('#fb_position').val();
var feedback = $('#fb_feedbacknew').val();
var data = 'fb_uname=' + name +
'&fb_client=' + client +
'&fb_department=' + department +
'&fb_email=' + email +
'&fb_position=' + position +
'&fb_feedbacknew=' + feedback;
$.ajax({
type: "POST",
url: "feedback.php",
data: data,
success: function (data) {

$("#feedback").get(0).reset();
$('#fb_message').html(data);

//$("#form").dialog('close');
$("#flex1").flexReload();

},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
return false;

});

$("#fb_form").dialog('open');

}

 

Fotiman




msg:4343561
 1:13 pm on Jul 25, 2011 (gmt 0)

Are you calling feedbacknew multiple times? If so, then it will add another onclick listener to #fb_submit each time feedbacknew is called, thereby causing duplicate posts.

ploppy




msg:4343579
 2:16 pm on Jul 25, 2011 (gmt 0)

hi. I am calling from a on:press and I am calling it once. For example, click->open dialog form->submit form-> close form. If i open a new form without refreshing the browser, then that is when it adds multiple entries. Thanks

Fotiman




msg:4343593
 3:20 pm on Jul 25, 2011 (gmt 0)

Then, as I would expect, you are attaching an onclick listener each time. I would move this bit of code to be outside of your feedbacknew function:

$('#fb_submit').click(function () {

var name = $('#fb_uname').val();
var client = $('#fb_client').val();
var department = $('#fb_department').val();
var email = $('#fb_email').val();
var position = $('#fb_position').val();
var feedback = $('#fb_feedbacknew').val();
var data = 'fb_uname=' + name + '&fb_client=' + client + '&fb_department=' + department + '&fb_email=' + email + '&fb_position=' + position + '&fb_feedbacknew=' + feedback;
$.ajax({
type: "POST",
url: "feedback.php",
data: data,
success: function (data) {

$("#feedback").get(0).reset();
$('#fb_message').html(data);

//$("#form").dialog('close');
$("#flex1").flexReload();

},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
return false;

});

ploppy




msg:4343641
 5:20 pm on Jul 25, 2011 (gmt 0)

@fotiman
that does not work. If I move out of function, it reloads the whole browser without drisplaying the addbox inside the dialog window. I need to keep the results showing in the dialog window. Thanks

Fotiman




msg:4343646
 5:31 pm on Jul 25, 2011 (gmt 0)

Ah, yes, my example would not have worked. Try this instead... add an unbind statement before you set the click handler to remove any previously bound handlers:

$('#fb_submit').unbind('click');
$('#fb_submit').click(function () {
...

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved