views:

61

answers:

2

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)

+1  A: 

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();
Husky
@Husky: Currently the tab is just a <a> link to the html page. No ajax or frames set up. Looking at jQuery .post http://api.jquery.com/jQuery.post/ not sure if this works like I think it does.
Tommy
Okay, so you want to submit a form on one page from another page? I don't think that will work, and i'm not quite sure what you're trying to do. Maybe you should try to setup a simple example so we can see what you're trying to do...
Husky
+1  A: 

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.

Sidnicious
Only know the concept of AJAX. Is this jQuery ajax post reliable? Does the server see this exactly the same as a POST from a form?
Tommy
Reliable? Absolutely. It should be functionally equivalent to submitting a form, as long as you match all the input names and values with keys and values in the `data` object.
Sidnicious
Great, it is in place and working well. Thank You. A side issue, I triend to change the cursor to "wait" at the beginning of this function and change it back in the success function. However, the cursor only changes to "wait" after the POST. Any idea? Thanks again for the ajax POST.
Tommy
This code works because it **blocks** on the call to `$.ajax`. The browser won't navigate to the new page until the request is finished, but it won't do anything else either — including changing the cursor. An normal, asynchronous call to `$.ajax` (`async` not set to false) doesn't block, so while you could change the cursor, it would take much more work (and code) to make the page behave well (i.e. doesn't let the user click on other links, or the same link again, while the POST runs).
Sidnicious