thirdearth Posted June 25, 2009 Share Posted June 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/163594-prevent-multiple-form-submits-with-php/ Share on other sites More sharing options...
Alex Posted June 25, 2009 Share Posted June 25, 2009 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()" Quote Link to comment https://forums.phpfreaks.com/topic/163594-prevent-multiple-form-submits-with-php/#findComment-863185 Share on other sites More sharing options...
thirdearth Posted June 25, 2009 Author Share Posted June 25, 2009 Thanks. The alert popping up is annoying, but the idea itself worked like a charm! Quote Link to comment https://forums.phpfreaks.com/topic/163594-prevent-multiple-form-submits-with-php/#findComment-863195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.