Edit 25.07.2018: As Pawnesh Kumar said in the answer, this seems to be a browser issue. If I hit the button multiple times in Firefox the below script will only send one POST
request, but if I do the same in Chrome I get a POST
request for each click.
However, I can replicate the problem in the video at 01:00. This means when I install Laravel with Authentication, then if I click the submit button in the login form twice, Firefox will send 2 requests.
Why is Firefox sometimes sending multiple POST
request and sometimes only one, when clicking multiple times on the button?
I have a user table
id | name
1 | John
where the field id
is a primary, integer, auto-incremet key. When I submit a dummy form that only has one button, then this will insert a new record with name John
. Now this is what I observed:
If I submit the form once, go back in the browser submit it again, then I find two new rows in the DB.
If I submit the form by clicking twice (or twenty times) on the
Add
button, then there is only a single new row in the DB.
Why is that? I would expect that if I hit the submit button multiple times, then the form will send multiple requests - and inserts multiple rows.
Thats my form:
<form action="/test.php" method="POST">
<input type="submit" value="Add">
</form>
which submits to test.php
:
<?php
$servername = "localhost";
$username = "adam";
$password = "password";
$dbname = "test-db";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "INSERT INTO user (name) VALUES ('John')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}
$conn->close();
sleep(4);
Because of the sleep
part, I can click on the Add
button multiple times in a row. However, no matter how often I click the Add
button while loading, there is only one new row in the DB.
In my access.log file I also find only one GET
and POST
request after clicking the button twenty times:
2001:****:****:4400:****:****:****:**** - - [25/Jul/2018:11:30:03 +0200] "GET /test/form.php HTTP/1.1" 200 301 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0" "*******.net"
2001:****:****:4400:****:****:***:**** - - [25/Jul/2018:11:30:34 +0200] "POST /test/test.php HTTP/1.1" 200 31 "http://********.net/test/form.php" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0" "********.net"
Remark:
I have read about techniques to prevent multiple submissions either in the backend or frontend. Thus I think it should be possible to submit the form multiple times by hitting the button multiple times.
I also read in the wiki article Post/Redirect/Get that this pattern can not prevent if a user sumbits a form multiple times too quick:
If a web user refreshes before the initial submission has completed because of server lag, resulting in a duplicate POST request in certain user agents.
Also in this video at 1:00 someone double clicks on a button and gets an error because he submitted twice.