最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I refresh the page when the submit button of a form is clicked? - Stack Overflow

programmeradmin7浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 2

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 );


}

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

发布评论

评论列表(0)

  1. 暂无评论