Skip to content Skip to sidebar Skip to footer

Submitting An Html Form Without Opening A New Window

So I know that when you put a URL in the action of an HTML form it will send the users to that url. I also added in the target attribute target='_blank' to open the new URL in a ne

Solution 1:

another solution, besides the ajax obvious one is using an inner hidden iframe as the target property of the form element. this way the form will open in a hidden inner element. e.g.:

<form id="eFrm" target="ifrm1">
</form>
<iframe id="ifrm1" name="ifrm1" style="display:none"></iframe>

Solution 2:

Here is what you need, simply adjust it to your needs, create a php file that gets called within the action and let it do what you would do with your form - exactly the same way, but stay on the same page:

$("#ajaxform").submit(function(e)
    {
        var postData = $(this).serializeArray();
        var formURL = $(this).attr("action");
        $.ajax(
        {
            url : formURL,
            type: "POST",
            data : postData,
            success:function(data) 
            {
                //data: return data from server
            }

        });
        e.preventDefault(); //STOP default action
        e.unbind(); //unbind. to stop multiple form submit.
    });

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

You need to include jQuery to your file to use this!


As you are using GET in your form - with this submission you will get the data in your $_POST.

Solution 3:

AJAX request suggested by baao is the way to go, but it has a caveat. The browser won't allow to pass your AJAX request if the target server is on a different domain. To solve the problem it should provide correct CORS headers.

If target server is out of your control, you can make a proxy on your own server and call it instead of the current 3rd-party server. This will allow you to simplify things even further by making proxy at the same address as the page with the form.

Post a Comment for "Submitting An Html Form Without Opening A New Window"