2
I have a form that after being submitted presents the result on a second page, like:
<form method="post" id="form" action="form.php">
<input name='nome' id='nome'>
<input type='submit' value='Enviar'>
</form>
And in the form.php
something like:
$nome = $_POST['nome'];
echo "O nome é $nome";
In this question is answered how to reset the data when the request is processed on the same page, but I need the data that has been filled out to be completely reset if the user goes back to page by browser button or using keyboard. I tried to:
<script>
$('#myForm')[0].reset();
</script>
But it didn’t work. What I need is that if the user goes back to the page, the field is no longer filled in. The form has several types of fields (select, input, checkbox, radio), and I need to reset all.
I read several questions on Soen, but due to the language barrier I was even in doubt about it is even possible.
Is it possible? How?
If it is post if you come back will ask you to resend the form. I solved this by sending only by ajax and when sending the form, save in the history the form page. You go back and it reloads the previous page. I’ll search and pass you the code.
– Willian Coqueiro
No @Williancoqueiro, I think you’re confusing... when you return the browser not ask anything.... What the browser asks is when the user gives F5 in a form that has already been sent e.g. or when using a JS/Jquery to warn that coming back from the form page (not yet sent) will lose the data and such... In case what I need is to delete the entered data when the user back with browser button (or on the keyboard), after giving Submit...
– gustavox
Depends on the browser. More can use ajax Ué already resove then. Captures the fields by Javascript and reset before sending. By the right. '$('form'). Submit(Function()' $(this)[0].reset(); });'
– Willian Coqueiro
I’m not using ajax, it wouldn’t work for me... but thank you for the strength...
– gustavox
You will need to use Javascript. And you cannot reset before submitting this form. You can use Javascript to change the history after sent on the next page. It works perfectly too.
– Willian Coqueiro
Yes @Williancoqueiro, I think it would really work! Does an answer with example happen? Thanks!
– gustavox
Use like this:
window.history.replaceState(msg, "Prefeitura Municipal ???????????", "index.php");
In your case it should be used after sending the request. Problem that this way, it will delete all history that you have traversed on the host and leave only this. For me decided to inhibit the user to return, in case he returns, always go to the home page. There’s a way to change the history of a page in the case may be the previous one, but I can’t remember why it didn’t work. But if this doesn’t solve your case, of a searched for: Change history with Javascript.– Willian Coqueiro