Jump to content

Prevent multiple form submits with PHP


thirdearth

Recommended Posts

Hi there.  I've been working on a PHP-based web game for quite some time that is coming out great, but I have run into a problem.  A lot of the aspects of the game involve clicking "submit" form buttons, and use a PHP isset command to update the mysql database based on what you clicked.

 

Example if I was trying to go North:

 

<form name="form" action="playerhome.php" method="post">
<input type="submit" name="north" id="north" value="North" />
</form>
<?php
if (isset($_POST["north"])) {$longitude--;
$herodirection = "heroup";
mysql_query("UPDATE members SET herodirection = '$herodirection', y='$longitude'  WHERE login = '$login'");
header('Location: playerhome.php'); }
   }
?>

 

Its more complicated than that, but not relevant to my question.  The problem is that while everything works great if you single click around in the game, problems arise when people click any buttons in rapid motions and start getting multiple clicks in before the form refreshes the page.  I thought I could use the javascript disable function to disable the button after it was pressed, but when I did that, any PHP within the isset command would no longer run.  Can anyone think of a way with PHP to prevent multiple submits?

Link to comment
https://forums.phpfreaks.com/topic/163594-prevent-multiple-form-submits-with-php/
Share on other sites

Here's what you can do, JavaScript will work.

 

var form_submitted = false;

function submit_form ()
{
  if (form_submitted)
  {
    alert("Your form has already been submitted. Please wait...");
    return false;
  }
  else
  {
    form_submitted = true;
    return true;
  }
}

 

Then within your <form> element add onsubmit="return submit_form()"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.