0

I was looking at this POST, and i was wondering, is there a way to make this function work as it should but without redirecting to a different page?

function post_to_url(path, params, method) {
    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);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }

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

My problem is on this line form.setAttribute("action", path); which sent's to a different site

Community
  • 1
  • 1
Wind64bit
  • 7
  • 2
  • 1
    Simply make an AJAX call that's sending the form data. – Shomz Mar 09 '14 at 15:52
  • Can you please explain a bit more? – Wind64bit Mar 09 '14 at 15:54
  • 1
    http://youmightnotneedjquery.com/#post – bbuecherl Mar 09 '14 at 15:54
  • Okay i see thank. one last thing is there any difference between any of them – Wind64bit Mar 09 '14 at 15:57
  • Learn how AJAX works, and it will all get clear. Basically, it's an asynchronous call (it happens 'behind' the page), but you can wait for its response and modify your page accordingly based on that response. There's much more to it, but this will get you started. See here: https://developer.mozilla.org/en/docs/AJAX – Shomz Mar 09 '14 at 15:59
  • Thanks a lot, you solved my problem, if you want you can post it as an answer so i can accept – Wind64bit Mar 09 '14 at 16:05
  • You're welcome, man. I'm not sure if it's going to help anyone else (if not, you should probably delete the whole question), but I've posted it as an answer. If you decide it's not going to help someone else, feel free to delete it. – Shomz Mar 09 '14 at 22:31

3 Answers3

0

You can use ajax to send the request. With jQuery it can look like this.

$.post( "test.php", $( "#testform" ).serialize() );
  • 2
    Your answer's a bit short. Also, linking to an external site is discouraged in answers due to the fact that that link might break at some time in the future. Please expand your answer with the relevant code. – Agi Hammerthief Mar 09 '14 at 15:57
0

After fixing it within comments, here is the answer version.

Instead of using a form, you need to make an AJAX call that's sending the form data. Learn how AJAX works, and it will all get clear. Basically, it's an asynchronous call (it happens 'behind' the page), but the good thing is that you can wait for its response and modify your page accordingly based on that response (for example, showing a 'thank you' message, or an error, etc...).

There's much more to it, but this will get you started. See here: https://developer.mozilla.org/en/docs/AJAX

Also, there are millions of examples and tutorials online, so you won't have any problem doing it now that you know what you need to do.

Shomz
  • 36,161
  • 3
  • 52
  • 81
-1

I think iframes may help you. Tryout:)

Shovan
  • 183
  • 7