views:

1589

answers:

4

I found this thread which basically has the same issue I have. But their solution is not working for me.

The dialog appears the first time I click the submit button, but not the 2nd time. I'm opening the dialog box after a form submission.

UPDATE

I finally got it working. Here is the correct code:

   if (jQuery('#registrationforms').length > 0) {
     //instantiate the dialog 
     jQuery("#dialog").dialog({ modal:true, autoOpen:false });

     //Some more code here to call processRegistration function.
   }

  function processRegistration(instanceID, formData)
  {

    jQuery.post("mypath/jquery_bll.php", { instance: 'processRegistration', formData : formData, instanceID : instanceID },
      function(feedback)
      {
        jQuery('#dialog').text(feedback.message);
        jQuery('#dialog').parent().addClass(feedback.type);
        jQuery('#dialog').dialog('open');
      },"json");

  }

Since I'm dynamically applying css class, I have to make sure to add it to the outer DIV which $.dialog creates to wrap my 'dialog' DIV.

+2  A: 

I think the highly voted answer by RayLehman in the post that you referenced is the correct solution.

jQuery UI's dialog() function actually creates a dialog out of some content. You aren't actually calling "open" on the dialog anywhere.

Once the dialog is created by calling dialog() the first time, you just need to call dialog("open") or dialog("close"), rather than re-creating the actual dialog object every time.

womp
Womp is right, ypu have to esplicitely open the dialog every time you need it.
Davide Gualano
I'm doing that now with my revised code. All I'm missing now is to add the correct class depending on the feedback type.
Steven
A: 

This sounds like you've wrapped your open event up with the init call. You need to make sure that you initialise your dialog first - typically setting the autoOpen property to false - and then have a separate click event for to open your dialog.

Read this article to explain it in detail.

Phil.Wheeler
A: 

Don't use dialog() to both initialize the dialog and open it at the same time. I made this mistake too.

First initialize the dialog, then open it in the callback like so:

jQuery('#dialog').dialog({ autoOpen: false });

function processRegistration(instanceID, formData) {
jQuery.post(...,
  function(feedback) {
    var dialog = jQuery('#dialog');
    dialog.text(feedback.message);
    dialog.addClass(feedback.type);
    dialog.dialog('open');
  }, "json");
};
a paid nerd
dialog.attr('class', feedback.type) will overwrite default classes. I need to ADD a class. And how would you open the dialog box?
Steven
Ooops! Edited. (attr() -> addClass(), added open)
a paid nerd
Ok, thanks. I still get the same result as with my updated code above. And I stil have the same prolem - the css for the added class is not rendered.
Steven
That would almost work. The css class had to be applied to the parent div. See my solution.
Steven
I'm accepting your answer as the right one, since you provided me with example code :)
Steven
Ahh, cool. Thanks :)
a paid nerd
A: 

This is what you are exactly looking for: http://praveenbattula.blogspot.com/2009/08/jquery-dialog-open-only-once-solution.html

Rare Solutions