1

I want to call a function when submit finish. The submit open a pdf in a new tab.

$(form).submit() // fire the submit 

but when I tried this approch

$(form).submit(function(e){            
        callfunction();
    }
);

The submit doesn't fire.

Here is my code.

<form id="form" action="#" method="post" target="_blank">
        <input type="hidden" name="docId">            
</form>

function getSubmitAttribute(data) {
    $form = $("#form");
    $(form).find("input[name=docId]").val($(data).parent().attr('id'));
    return form;
}

$('#datable tbody').on('click', '.cell', function () {
    form = getSubmitAttribute($(this));
    $(form).attr('action', url);
    $(form).submit();
}

Thank you.

Mercenaire
  • 164
  • 11

1 Answers1

0

The way I've done this in projects is to have the response containing the PDF set a cookie (since I can't have it run code, it's not a web page). Then I can poll for the cookie in the page sending the form. In pseudo-code:

$(form).submit();
var timeout = Date.now() + TIMEOUT_IN_MILLISECONDS;
poll();
function poll() {
    if (theCookieValueExists()) {
        // Done
    } else if (Date.now() > timeout) {
        // Give up
    } else {
        // Keep waiting
        setTimeout(poll, POLL_INTERVAL);
    }
}

I send the cookie name and value with the form, ensuring that they're (very likely to be) unique, so that if this is happening in multiple windows the code doesn't get confused.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • Thank you but I think that I can not read a cookie with jquery created by php (Symfony) – Mercenaire Feb 07 '18 at 10:47
  • @Mercenaire: I'm afraid you're incorrect there. :-) You can indeed use PHP to create a cookie, which can then be read by your JavaScript code (jQuery doesn't help you read cookies, though [there's a plugin for it that does](https://plugins.jquery.com/cookie/)). – T.J. Crowder Feb 07 '18 at 10:49
  • I think JavaScript too ? I'm going to search how – Mercenaire Feb 07 '18 at 10:51
  • @Mercenaire: JavaScript code on the browser can read cookies set by PHP once the response from PHP has been received by the browser. (Unless you go out of your way to prevent it, by making the cookie "HTTP only".) – T.J. Crowder Feb 07 '18 at 10:54
  • 1
    I receive it well in my chrome browser, it only remains to know how to read it in javascript – Mercenaire Feb 07 '18 at 11:00
  • @Mercenaire: Good good. The subject of how to read cookies is **well** covered on the 'net. (Or you could use the plugin referenced above.) – T.J. Crowder Feb 07 '18 at 11:05
  • @Mercenaire: Nice one! – T.J. Crowder Feb 07 '18 at 13:11