T.T.T.
T.T.T.

Reputation: 34523

How do I submit a form from another page?

I have a two page site. Each page contains a header with "tabs" at the top, to navigate between page 1 and 2.

When a tab is selected, I need the selected page to re-POST to the server, to quickly refresh before it is displayed.

I have a form on each page that allows a manual refresh from that page but need to submit that form from the other page.

For example, page 2 is displayed. The tab for page 1 is selected (which is on page 2) It should then submit the refresh form on page 1.

Any help is appreciated.

(Using jQuery, js, html and CSS)

Upvotes: 1

Views: 2931

Answers (2)

s4y
s4y

Reputation: 51685

Ideally, the refresh should be done by the server when the page is loaded. If that isn't an option, I would use jQuery's $.ajax method in synchronous mode, attacked to the tab's click event:

$('a.tab').click(function() {
    $.ajax({
        url: "/refresh",
        method: "post",
        async: false,
        data: { refresh: "very yes" }
    });
});

Setting async to false ensures that the request finishes before the click handler returns and the new page starts to load. You will need to include he form data to be submitted in the data object, it's not feasible to post a form located on another page.

This is actually almost identical to the method Google Analytics uses to track clicks on outbound links.

Upvotes: 2

Husky
Husky

Reputation: 6186

How are you actually loading your pages from the tabs? Using some kind of iframe? Or by doing an AJAX call? Using frames it might be more trickier than using an AJAX-call.

If your form has an id or a class you can simply use jQuery's submit() function:

$("#myform").submit();

Upvotes: 1

Related Questions