Submit form twice or refresh two times - php

Hi I am using php and i want to submit form on onclick event of radio button.
when form submission is done, than after I want to reload my page.
Means first form is submitted and after form submit I want to reload my page again.
Is it possible?
I have tried it with javascript but not get any idea how to do it..
Thanks in advance.

header('Location: http://url.to/reload');
exit;
Make sure to do this before any output on that page otherwise you'll get an error about headers already having been sent.

Related

prevent dupplicate submitted by ajax

I'm using ajax to create list of replies.
In the end of the list I added textarea form that allow user to add reply (also with ajax).
The problem is that i call to my JS in my main PHP page so when user want to submit reply the page doesn't "know" the js code. If i add the js to the ajax php file/page the code will work but then it will be duplicated many times and when user will submit form the text will be submitted many times...
In my console i see that the JS file duplicate every time i load the replies list
How can i prevent it?
Disable the submit button right after user presses it once. Using jQuery:
$("#button").attr('disabled','disabled');
Make sure to remove disabled attribute on AJAX error so user can re-submit the form with new data. You can remove disabled attribute like this:
$('#button').removeAttr('disabled');
[improved]
if you want to not repeat data when navigation or F5 press, simply free the $_POST vars after doing whatever you want, and check if isnt set (before clean, of course) redirect you wherever you want. example:
/* HERE do the job with ajax response. */
if(!isset($_post['foo'])) header('Location: wherever.php');
$_POST['foo']=NULL;
If you're using $_GET... don't do it, use $_POST... $_POST is your friend.
Finaly ensure that if you press F5, you don't re-send form vars by post (otherwise you will get the same). If it's happening, clear your inputs with jQuery on page load.
$('#inputID').val('');

How to prevent browser's forward button on chrome from submitting a form?

I have a simple php form that submits (POSTS) data on pressing the SUBMIT button and a 'thank you' page is displayed and the data is stored in a database.
Usually on this thank you page if you press the BACK button on the browser and then the FORWARD button on the browser you are brought back to the same thank you page but the form is not submitted again.
In the last few days when I do the BACK and FORWARD on the browser the form resubmits the data and there's a duplicate entry in the database. This happens only in Chrome.
Have I made some errors in the settings in Chrome or is there some other problem somewhere?
The typical solution is known as POST–Redirect–GET. Essentially, your form posts to a page which inserts the data into the database or whatever other actions are necessary and then redirects to another page. That other page doesn't actually do anything but just displays a success message or something. This way, you have two entries in the history: the form and the success page. The form-posting page is never added to the history; pressing back or forward will skip the submission.
Generate a value and put that inside a hidden field. If the user submits the form store that value (must be unique). If one tries to submit the form again with the same generated value, then do not execute your insert or update.
You could set a cookie or session that says the form has already been submitted, and, if that is set don't resubmit the form, but that is basically a band-aid and may not even work...
What you should REALLY be doing is avoiding duplicates by checking the input values against existing values in the db, such as email or username. You should also set your email and username fields to UNIQUE in your database so you'll never get duplicate email addresses or usernames - solving your problem.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')) {
// 1. check if $_POST['email'] already exists in the database
// 2. if email doesn't exist, insert data
}
Th issue is, when you reached the success page and refreshed, the browser has to resubmit the cached data; because the page where it reached is dynamically generated. Now when you click on the okay, the data which was previously stored in the $_POST variable is resubmitted. To stop it, you would have use dynamic binding instead of static binding.
A simple solution to this issue is:
Make the action attribute of the form blank i.e <form action="">.
Call a javascript method onclick of the intended button.
Add the Action attribute in the JS method and submit the form.

Pressing enter in textarea field refresh the page

I'm using Symfony2 but i'm experiencing a troubling thing.
I have a form, with a textarea field, but each time i press 'enter' to go to the next line it refresh the page (without deleting the previous line)..
Do you have any idea where does it come from?
Thanks!
Well for sure you have a submit button on the form. On enter in a form you call the submit butt that refreshes the page!
In order to prevent that you can just make the button in the type="button" and bind a onclick function so that the submit button is not called on enter!
However; you will remove the enter feature that calls the submit on the entire form not just in the text area!
OR --> you can just call a function on keypres or keyup on the text area and e.prevetnDefault();
I hope my answers helps you!

how to disable cofirm form resubmission

Hi all I have a search form which is using the post method and it is redirecting the results into a div on a another page but when I refresh the page I get this confirm form resubmission dialog box and I was just wondering to know how can I disable this thanks.
I always use a header('location:http://url'); exit(); after a page use a POST. If you refresh it will not re-execute or ask for form resubmission. But you need to pass data to the next page, like with sessions.

Please help me out how to submit button value only on button click not on page refresh

I'm using form in a project. In my form, when I click on submit button, the values are submitted properly but after submit if I refresh the page the existing values are inserted again without clicking on button. Please help me out how to submit the button value only on button click not on page refresh.
Use a redirect after page submit to a thank you page.
Header('location: thankyou.php');
exit();
Use the post-redirect-get pattern. http://en.m.wikipedia.org/wiki/Post/Redirect/Get
This means you use POST method on the form, and in the response script you redirect the user to a different page with the location header.
There is a good answer to your question here: How to avoid duplicate when a user click the "refresh" button of his browser?

Categories