Disable Element by PHP if-statement

Hey, as the summary already states, I want to disable an HTML-Element with PHP if an action occurs. For example I have a simple input button, and if the input button has been clicked 5 times, it is being disabled.

If anyone has an idea, please help.



You’re going to have to supply some more details. Is the input button going to refresh the page? If so, we can count the clicks with PHP and then disable the button by adding the “disabled” attribute with PHP before spitting the HTML out. If not, we need to count with JS and PHP has nothing to do with this, and we can disable the button with JS.

What the point of this is might help as well.

      if ($_POST['rock'] * 5) {
          // action for disabling the input class ".inputButtons1"
      else {

Inside the if-statement, it should somehow work to disable the element with jQuery or JavaScript, and else do nothing.

if you want to use javascript, you can get php to echo out the javascript to set the element’s “disabled” property to true to disable it or false to enable it.

And that’s what I was trying to do, but don’t know how…

This w3schools tutorial might help.

This wouldn’t work, since I can’t disable it right away after it has been clicked. I need to disable it by an PHP if-statement, and as you said printing out the JS or jQuery by an echo is more useful…

yep, that tute just shows how to use javascript to disable/enable elements.

In your application you would have to get php to echo out the appropriate javascript, based on the tute, in an IF block or whatever.

Otherwise, if you are refeshing the page on each click and don’t want to use javascript, you can count the clicks in PHP (as opposed to counting them in javascript) and then after a certain number of clicks (stored in a session variable) you can get php to not output the html to display the element.

Personally, if each button click is refreshing the page I wouldn’t bother using javascript at all and just store the click count in a session variable as mentioned above.

I have forms on my website that make it impossible to click the submit button if certain form elements don’t validate. I think this is the appropriate way to limit submissions. If you have somebody that keeps submitting again and again, you should try setting a form token, which involves a cookie/session. Also, you will need to validate all of your form input with php, and re-populate the form as needed.