0

I am trying to build a PHP webpage with the following behaviour:

1- A client access the webpage (that contains some buttons);

2- When the webpage is loaded, the PHP script opens a file stored on the server and, based on the information in this file, enables/disables some of the buttons, so that the client can see the webpage with the correct buttons enabled or disabled.

To enable/disable buttons, I know I can use javascript, while to read the file on the server I use PHP as stated above.

How do I put the two things together? Or should I use a PHP code equivalent to the following javascript line:

<script>document.getElementById("button1").disabled = true;</script>

At first I thought that inserting this line in the PHP code was the solution, but then I found out that this can't work for obvious reasons.

Thanks for the help!

Is it correct if I add the following javascript function in the head section of my webpage?

<script>
function enableButtons() { 
<?php 
if($state=="state1") {
  echo 'document.getElementById("button1").disabled = true;';
} 
else if($state=="state2") { 
  echo 'document.getElementById("button2").disabled = true;'; 
} 
?>  
} 
</script> 

I call the enableButtons() function when loading the page by using

<body onload="enableButtons()">

The php code above is just an example, the number of states and buttons is higher, that's why I would like to use this solution.

4
  • 1
    You can use ajax to call PHP scripts from javascript. Commented Apr 26, 2016 at 15:06
  • 1
    It looks like you are creating the page on the server where the file is located and you want to disable the buttons. If that is the case, when you print the HTML for the button, include the attribute to disable it. No need for JavaScript at all. Commented Apr 26, 2016 at 15:07
  • You can use PHP to modify the markup before it is sent to the client. And please explain the nature of the information in this file and a sample of the button's markup. Commented Apr 26, 2016 at 15:08
  • I need to create a simple webserver for a machine that can be accessed by different clients (so we are not talking about a website and I can't use a cookie). When client1 access my index.php and clicks some buttons, I would like to save that information, so that if client1 closes the browser and client2 opens a new connection to my server, my "index.php" appearance is changed accordingly to which buttons client1 clicked. Eg if client1 clicks "Start", client2 must not be able to click "Start" anymore. I tried to update my initial post based on what someone recommended. Commented Apr 27, 2016 at 12:43

3 Answers 3

1

The common thing to do is to have php read the settings file, and echo the "disabled" attribute on the buttons before sending the output to the user browser. You can get more info about the attribute here here. You do not need javascript.

Do something like this:

<button type="button" <?php if($state === 'state1') echo 'disabled'; ?>>Button text</button>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Nice one! I edited my original post if you can have a look.
You are using javascript, and you shouldn't for this. What happens if the user browser has javascript turned off? Anyway, If that is your only option, it looks ok. I have updated my answer with the condition you have
I re-arranged my code in order to use html only, following your suggestion, and I see that your solution is better and cleaner. Thank you
You're welcome, please mark it as correct answer to help future users
0

Usually you send to the client the buttons already disabled and use js to respond to any event that happens after sending the page, like selecting a combo box value.. You can omit the code, using an if sentence, or hide them using css. First approach is preferred.

Comments

0

Script

<script>
  function isValid(f){
    if(f.test.value==''){
      alert('please enter name');
      return false;
    }else{
      $(".bbutton").html("Processing please wait");
     return true;
    }
  }
</script>

HTML

<form method="post" onsubmit="return isValid(this);">
   <input type="hidden" name="te">
   <input type="text" name="test">
   <div class="bbutton">
      <input type="submit" value="send">
   </div>
</form>

When you submit the form then it will automatically hide the submit button to avoid pressing again and again, and you can redirect it to other page. May be this idea helpful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.