Open url when passing POST data using jQuery

Is it possible to use jQuery to change the URL of a page when transferring data to a new page?

+5
source share
4 answers

If you want to change the current page URL , then you can add a new one <form>to the current page, add hidden input elements to it, and then send 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 similar

+12
source

Try the following function. It works for me. JSON compatible.

/**
 * Function that will redirect to a new page & pass data using submit
 * @param {type} path -> new url
 * @param {type} params -> JSON data to be posted
 * @param {type} method -> GET or POST
 * @returns {undefined} -> NA
 */
function gotoUrl(path, params, method) {
    //Null check
    method = method || "post"; // Set method to post by default if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    //Fill the hidden form
    if (typeof params === 'string') {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", 'data');
        hiddenField.setAttribute("value", params);
        form.appendChild(hiddenField);
    }
    else {
        for (var key in params) {
            if (params.hasOwnProperty(key)) {
                var hiddenField = document.createElement("input");
                hiddenField.setAttribute("type", "hidden");
                hiddenField.setAttribute("name", key);
                if(typeof params[key] === 'object'){
                    hiddenField.setAttribute("value", JSON.stringify(params[key]));
                }
                else{
                    hiddenField.setAttribute("value", params[key]);
                }
                form.appendChild(hiddenField);
            }
        }
    }

    document.body.appendChild(form);
    form.submit();
}

Demo version

    <script>
        $('#GotoNextPage').on('submit', function(evt){
            evt.preventDefault();

            //Make the data
            var sampleData = new Object();
            sampleData.currentPage = 'We are here';
            sampleData.currentUid = 5;

            gotoUrl("actionPage.php", {'cmd':'nextPage', 'data':sampleData});
        });
    </script>

Hope this helps!

+4
source

POSTING not ( ), , jQuery.post() method :

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

This seems like an unlikely situation, maybe you could comment on more detailed information about the transmitted data and its relation to the content of the page?

+2
source

Save variables in $_SESSIONusing jquery post method

Jquery code

$.post("save_session.php", { out: output }) .done(function(data) {  
location.href='your_link.php';
});

Php Code (save_session.php)

$_SESSION["variables"]=$_POST["out"];

Php Code (your_link.php)

$output=$_SESSION["variables"];
+1
source

All Articles