Page 1 of 1

2 questions

Posted: Fri Jan 13, 2006 11:22 am
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 :(

Posted: Fri Jan 13, 2006 11:28 am
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.

Re: 2 questions

Posted: Fri Jan 13, 2006 12:25 pm
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.

Posted: Fri Jan 13, 2006 3:51 pm
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; ?> />

Posted: Fri Jan 13, 2006 3:59 pm
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 } ?>

Posted: Fri Jan 13, 2006 9:49 pm
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.

Posted: Sat Jan 14, 2006 6:10 am
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>

Posted: Sat Jan 14, 2006 10:25 pm
by wtf
Here's interesting tute regarding #2

http://www.oreilly.com/catalog/phphks/c ... hack55.pdf

Posted: Sat Jan 14, 2006 10:59 pm
by Afterlife(69)

Code: Select all

onClick="this.disabled = true;"