2 questions

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

2 questions

Post by s.dot »

#1) how can I disable a form button when it has been clicked/submitted

I know it's javascript.. like this.form.buttonname.disabled='true'
something like that

#2) is it possible to have people not be able to refresh a page, or if they do... not to repeat the $_POST action. I could use an intermediate page but that would be a lot of work :(
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

Code: Select all

<input type="button" value="go" disabled>


You could use hidden value inside your form to prevent it from submitting again.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: 2 questions

Post by Roja »

scrotaye wrote:#1) how can I disable a form button when it has been clicked/submitted

I know it's javascript.. like this.form.buttonname.disabled='true'
something like that

Code: Select all

<form name="forma" action="admin.php" method="post" onsubmit="document.forma.submit_button.disabled=true;">
scrotaye wrote:#2) is it possible to have people not be able to refresh a page, or if they do... not to repeat the $_POST action. I could use an intermediate page but that would be a lot of work :(
Use the Post/Redirect/Get pattern for web applications: http://www.theserverside.com/patterns/t ... d_id=20936

In summary, you basically have the post results go to a processing page, and upon processing, you redirect to a "change made" page. If they back button, it goes to the original form (not the processing page), and if they refresh, it just redisplays the "change made" (without making a change). Best of both worlds.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

if javascript is disabled, you can follow this approach

Code: Select all

if ($_POST["submit_button_name"] != "" || $_POST["submit_button_name"] != null){
$disabled = "disabled";
}else{
$disabled = "":
}
<input type = 'submit' name = 'submit_button_name' value= 'submit' <?php echo $disabled; ?> />
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

for your second problem...this should be a fix...report errors if you face some...

Code: Select all

<?php

if ($_POST["submit_button_name"] != "" || $_POST["submit_button_name"] != null){ 
	$disabled = "disabled"; 
	if ($fields_are_valid){
		$_SESSION["form_submitted"] == TRUE;
	}
}else{ 
	$disabled = "": 
} 
?>
<?php if ($_SESSION["form_submitted"] === TRUE){ ?>
<input type = 'submit' name = 'submit_button_name' value= 'submit' <?php echo $disabled; ?> /> 
<?php }else{ ?>
echo "Form has already been submitted. Provide a redirect link here!!!";
<?php } ?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Thanks guys.

I went a different approach with the form thing, but it works the same.

Code: Select all

function disableForm(formname) {

formname.Submit.disabled=true;
formname.Submit.value="Wait...";

}


<form name="myForm" onSubmit="disableForm(myForm);">
And for the second problem, i went ahead and redirected to an intermediate page, like Roja mentioned.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I read the PRG pattern referred by Roja. It looks good, I think something like this should be done instead of constructing individual redirection pages.

one file confirmActions.php

Code: Select all

<?php
$action = $_GET["action"];
$actionComment = "";
if (action == "editPostSuccessful"){
	$actionComment = "Post Successfully Edited";
}else if (action == "createNewPostSuccessful"){
	$actionComment = "New Post Sucessfully Created";
}else if (action == "deletePostSuccessful"){
	$actionComment = "Post Successfully Deleted";
}else if (action == "createTopicSuccessful"){
	$actionComment = "Topic Successfully Created";
}else if (action == "editTopicSuccessful"){
	$actionComment = "Topic Successfully edited";
}
?>

<div>
	<?php echo $actionComment; ?>
	<?php if ($_GET["objectTitle"]) echo "-".$_GET["objectTitle"]; //provide title of object on which action is performed ?>
</div>
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

Here's interesting tute regarding #2

http://www.oreilly.com/catalog/phphks/c ... hack55.pdf
Afterlife(69)
Spammer :|
Posts: 14
Joined: Mon Oct 03, 2005 4:51 am

Post by Afterlife(69) »

Code: Select all

onClick="this.disabled = true;"
Post Reply