/    Sign up×
Community /Pin to ProfileBookmark

Disable Form Submiy

I have a form, I want it to submit to a page, but after i click submit i want to disable the ability to submit it again.

Is there a vanilla way of doing this? stopping the button & enter

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@codewitchJul 20.2021 — Hello. If I understand your problem correctly, you need php session variables. Those are variables whose values are shared among multiple pages. After user submits the form for the first time, you change value of a session variable and then use this code:
<i>
</i>&lt;form action="script.php" onsubmit="return false"&gt;
&lt;input type="text" name="stream" style="width:200px;"&gt;
&lt;input type="submit" value="send"&gt;
&lt;/form&gt;


Which disables use of the button again.

To answer your question, yes, you can use Javascript to disable the button, but I don't see a way how to use Javascript to disable the second attempt, that is why I suggested the use of the php session variables.
Copy linkTweet thisAlerts:
@kiwisauthorJul 20.2021 — No I just want to disable the ability to post the form once the post method has been activated via button or enter.
Copy linkTweet thisAlerts:
@codewitchJul 20.2021 — Here is your first file:
<i>
</i>&lt;!-- here is file index.php --&gt;&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"/&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;hey I am a title&lt;/title&gt;
&lt;style&gt;

<i> </i>&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php
// set a session variable
$_SESSION["numberOfClicks"] = "zero";

if($_SESSION["numberOfClicks"] == "zero"){echo '&lt;form action="script.php" method="POST"&gt;&lt;input type="text" name="stream" style="width:200px;"&gt;&lt;input type="submit" value="send"&gt;&lt;/form&gt;';}
if($_SESSION["numberOfClicks"] == "one"){echo '&lt;form action="script.php" onsubmit="return false"&gt;&lt;input type="text" name="stream" style="width:200px;"&gt;&lt;input type="submit" value="send"&gt;&lt;/form&gt;';}


?&gt;

&lt;/body&gt;
&lt;/html&gt;


and here is your second file:
<i>
</i>&lt;!-- here is file script.php --&gt;&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"/&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;hey I am a title&lt;/title&gt;
&lt;style&gt;

<i> </i>&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

$_SESSION["numberOfClicks"] = "one";
$text = $_POST["stream"];

echo 'User input is:'.$text;

?&gt;

&lt;/body&gt;
&lt;/html&gt;


The user cannot submits the button for the second time if they would click on return to the previous page in their browser.
Copy linkTweet thisAlerts:
@kiwisauthorJul 20.2021 — No, you should be able to do this in JS.

I've got PHP blocks in place but want a JS one as well
Copy linkTweet thisAlerts:
@codewitchJul 20.2021 — Here is your first file:
<i>
</i>&lt;!-- here is file index.php --&gt;&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"/&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;hey I am a title&lt;/title&gt;
&lt;style&gt;

<i> </i>&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php
// set a session variable
if($_SESSION["numberOfClicks"]!="one"){$_SESSION["numberOfClicks"] = "zero";}

if($_SESSION["numberOfClicks"] == "zero"){echo '&lt;form action="script.php" method="POST"&gt;&lt;input type="text" name="stream" style="width:200px;"&gt;&lt;input type="submit" value="send"&gt;&lt;/form&gt;';}
if($_SESSION["numberOfClicks"] == "one"){echo '&lt;form action="script.php" onsubmit="return false"&gt;&lt;input type="text" name="stream" style="width:200px;"&gt;&lt;input type="submit" value="send"&gt;&lt;/form&gt;';}


?&gt;

&lt;/body&gt;
&lt;/html&gt;


and here is your second file:
<i>
</i>&lt;!-- here is file script.php --&gt;&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"/&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;hey I am a title&lt;/title&gt;
&lt;style&gt;

<i> </i>&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

$_SESSION["numberOfClicks"] = "one";
$text = $_POST["stream"];

echo 'User input is:'.$text;

?&gt;

&lt;/body&gt;
&lt;/html&gt;


The user cannot submits the button for the second time if they would click on return to the previous page in their browser.
Copy linkTweet thisAlerts:
@kiwisauthorJul 20.2021 — @codewitch#1634413

Stop replying
Copy linkTweet thisAlerts:
@codewitchJul 20.2021 — Ok, so using Javascript only:
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Disable button using disabled property&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;Click the button to submit data!&lt;/p&gt;
&lt;p&gt;
&lt;input type='submit' value='Submit' id='btClickMe' onclick='save(); this.disabled = true;' /&gt;
&lt;/p&gt;
&lt;p id="msg"&gt;&lt;/p&gt;
&lt;script&gt;
function save() {
var msg = document.getElementById('msg');
msg.innerHTML = 'Data submitted and the button disabled.';
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@kiwisauthorJul 20.2021 — @codewitch#1634415

Doesn't stop a form being submitted via enter. There's no form on your code at all.
Copy linkTweet thisAlerts:
@codewitchJul 20.2021 — <i>
</i>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Disable button using disabled property&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;Click the button to submit data!&lt;/p&gt;
&lt;p&gt;
&lt;form action="script.php" method="POST"&gt;
&lt;input type="text" name="stream" style="width:200px;"&gt;
&lt;input type='submit' value='Submit' id='btClickMe' onclick='save(); this.disabled = true;' /&gt;
&lt;/form&gt;
&lt;/p&gt;
&lt;p id="msg"&gt;&lt;/p&gt;
&lt;script&gt;
function save() {
var msg = document.getElementById('msg');
msg.innerHTML = 'Data submitted and the button disabled.';
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;


Now there is a form.

If you press tabulator key on your keyboard to "activate" the button, it gets submitted after by pressing enter key.

But if a user reloads the page they can submit it again, that is why I think the php session variables are a better solution, depending on what you need.

EDIT: moderators please delete post number 4 in this thread, there is a corrected code in post number 6, thank you
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...