Common practice these days would be to use AJAX but this doesn't answer the question, nor would any Javascript solution without any assistance from the back-end. Say you have a form, for example:
<form action="foo.php" method="post">
<input type="text" name="myVariable"/>
<button type="submit">Submit</button>
</form>
When submitting to your PHP file for processing you will be accessing your form data either via $_GET
or $_POST
and doing whatever you need to do with it. At this point you will be able to deduce whether or not the form has been submitted or is successful - based on this you can simply flag either input
or button
elements with the disabled
attribute, here's a basic example:
<?php # foo.php
$myVariable = isset($_POST['myVariable']) ? $_POST['myVariable'] : '';
echo "You submitted: {$myVariable}\n"; // do something useful...
// if variable is not an empty string, ie. it has been submitted,
// set $disabled which can then be output in-line with your HTML
$disabled = ($myVariable != '') ? 'disabled' : '';
?>
<form action="foo.php" method="post">
<input type="text" name="myVariable"/>
<button type="submit" <?php echo $disabled; ?>>Submit</button>
</form>
This would then output your new form with the button disabled based on your conditions. I should also note that depending on your doctype
you may need to give the attribute a value for it to be considered valid markup, but generally speaking:
<!-- valid HTML5 -->
<button disabled>button</button>
<!-- valid XHTML -->
<button disabled="disabled">button</button>
Some related StackOverflow reading:
What is a doctype?
What does ?:
mean? (ternary operator)
PHP: How can I submit a form via AJAX (jQuery version)