For example I have this code:
<form name="formm" action="/" 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?
For example I have this code:
<form name="formm" action="http://example./" 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?
Share Improve this question edited Sep 3, 2014 at 16:38 callmexshadow asked Aug 19, 2014 at 19:23 callmexshadowcallmexshadow 511 silver badge6 bronze badges 2- 1 Just set action to empty. <form action="" /> – psdpainter Commented Aug 19, 2014 at 19:25
- Can't leave the action empty because I'm sending the data to another page (search query). – callmexshadow Commented Aug 19, 2014 at 21:14
4 Answers
Reset to default 2Within 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 );
}
You could try:
onsubmit="setTimeout(function () { window.location.reload(); }, 10)"
I use this for my forms and it works perfectly across all browsers :)
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.
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