callmexshadow
callmexshadow

Reputation: 51

How can I refresh the page when the submit button of a form is clicked?

For example I have this code:

<form name="formm" action="http://example.com/" target="_blank">
    <input type="text" name="txt" />
    <input type="Submit" value="Submit" />
</form>

When I click submit it sends the form to the link which opens in a new tab. This is exactly what I want to happen. However, I would also like my page to refresh so I can run some PHP code. Simple enough, I add this to my submit input:

onclick="location.reload()"

This seems to work in any other case except when it's added to the submit button. How can I get this to work?

Upvotes: 2

Views: 6898

Answers (4)

Tristan
Tristan

Reputation: 215

You could try:

onsubmit="setTimeout(function () { window.location.reload(); }, 10)"

I use this for my forms and it works perfectly across all browsers :)

Upvotes: 2

Dan Papetti
Dan Papetti

Reputation: 33

Within the form that is submitting to a new page, add onClick="reloadpage();". This works in all 5 browsers:

function reloadpage()
{

    var returnURL = "this_pages_name.php?upd=" + Math.random() * 100;

    setTimeout(function() 
    {
        window.location=returnURL; 
    }, 50 );


}

Upvotes: 2

Fueled By Coffee
Fueled By Coffee

Reputation: 2559

In your PHP before generating any output declare a header to move location to current file:

header('Location: .');

This will set header location to: '.' (current directory) and then look for your page again. It will also clear any form datasets preventing database spam

Upvotes: 0

Harry Beasant
Harry Beasant

Reputation: 1014

You could try;

$('#form_id').on("submit", function() {

  location.reload();

});

This shouldn't prevent the default action of the form being submitted, but should capture the event and reload the page.

You will need to specify an ID on the form.

Upvotes: 1

Related Questions