tags:

views:

54

answers:

2

Is it possible using jQuery to change the page URL while passing post data to the new page?

A: 

If you mean that you want to change the current page URL, well then you can add a new <form> to the current page, add hidden input elements to it, and then submit it.

$('body').append($('<form/>')
  .attr({'action': yourNewURL, 'method': 'post', 'id', 'replacer'})
  .append($('<input/>')
    .attr({'type': 'hidden', 'name': 'param1', 'value': "hello"})
  )
  .append($('<input/>')
    .attr({'type': 'hidden', 'name': 'param2', 'value': "world"})
  )
).find('#replacer').submit();

Or something like that.

Pointy
Is it possible to use jQuery.ajax?
Brian
Well if you want to replace the whole page anyway, what's the point of Ajax?
Pointy
Could I use .load?
Brian
@Brian - You want to **change pages**, ajax is specifically to prevent **changing pages**. What you want is a form with an action that goes to the new page, nothing more. You don't even need a lick of javascript to do this either, just a `<form action="/Page2" method="POST">`
Nick Craver
A: 

If you are POSTing data not in a form (because you could simply set the form's action to the current page) and the data being posted isn't directly related to the content of the next page, you can use the callback function of jQuery's .post() method to redirect the user:

$.post( '/Page2', { 'foo' : 'bar' }, function() {
    window.location.href = '/Page2';
});

This seems like an unlikely situation, maybe you could comment with more details about the data being passed and its relation to the content of Page2?

tb
I am have one form on my page and one form in a jQueryUI dialog box. I can't submit them both at once, so I am trying to post both of them using serialize to the target page.
Brian
That doesn't seem to work, the POST data isn't present when page2 loads
Brian