0

My jQuery script looks something like (the form is submitting files to upload):

      $("#addMore").click(function() {
                $("#progForm").submit();
                $("#upTar").css("display","block");
                $("#title").val("");$("#obj").val("");$("#theory").val("");     $("#code").val("");$("#output").val("");$("#conc").val("");
            });

I want to delay the execution of the code beginning from $("#upTar") until the form submission is completed (that is the files are uploaded and PHP script has responded).

Edit

#upTar is an iframe and the target of the form submission. I want #upTar to be displayed only after its content has been generated from the form action script.

Thanks!

Code after solution

 $("#addMore").click(function() {
    $("#progForm").submit();
    $('#upTar').load(function() {
        $("#upTar").css("display","block");
        $("#title, #obj, #theory, #code, #output,     #conc").val("");
       });
 });
2
  • 1
    If that's a real form submit, does the form have a "target" attribute or something, to ensure that the page containing it is not wiped out by the server response? If not, then there's no point doing anything on the page really. Commented Mar 28, 2011 at 14:18
  • The target is to an iframe (id:upTar). Commented Mar 28, 2011 at 14:24

3 Answers 3

2

There's no way to make JavaScript in a browser "wait" for anything. In this case, there are a couple things you could do:

  1. You could put the CSS changes etc. in a "load" event handler for the target <iframe>

    $('#upTar').load(function() {
            $("#upTar").css("display","block");
            $("#title, #obj, #theory, #code, #output, #conc").val("");
    }
    
  2. You could put code in the response to the form POST that executes from the <iframe> itself.

    $(function() {
      (function($) {
            $("#upTar").css("display","block");
            $("#title, #obj, #theory, #code, #output, #conc").val("");
      })(window.parent.$);
    });
    
Sign up to request clarification or add additional context in comments.

3 Comments

The problem is I am trying to upload a file giving a (pseudo)ajax effect as ajax does not support file uploads. I am doing this by submitting the form to a hidden iframe and then unhiding the iframe once the response is generated. Therefore, 1. iframe has to be already loaded 2. iframe cannot be generated on the fly.
The browser should generate a "load" event on the <iframe> when the HTTP response completes. Have you tried that?
Oh good - I was pretty sure it would, but I was too lazy to check with a jsfiddle :-)
0

What you are doing right now is submitting the HTML form and loading whatever page you have listed in the "action" attribute of the form. The lower part of the javascript function will never actually execute since the browser will be directed to the new page. Here's what you want, a form that submits via ajax and then clears the form:

$("#addMore").click(function() {
    var formData = $("#progForm").serialize();
    var url = $("#progForm").attr("action");
    $.get(url, formData, function(){
        $("#upTar").css("display","block");
        $("#title").val("");
        $("#obj").val("");
        $("#theory").val("");
        $("#code").val("");
        $("#output").val("");
        $("#conc").val("");
    });
});

Were you looking for something like that?

2 Comments

...and the OP pulled the rug out from under me :P
I have tried using something similiar but to no avail. Moreover, I guess, get method does not support file uploading.
0

If you load jQuery inside the iframe you could use the following function to control parent elements with this selector:

$("#myparentid", top.document); 

In essence, you could run a function inside the iFrame that waits for a refresh at which point you can run any function you wish with the top.document selector.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.