1

I'm new to javascript. I have created a page that includes an add and a disabled unfriend button. Each button fetches a data id from the registration page.

The add button is a link to a prompt box. The data entered into the prompt box will be inserted into the database. Once completed, the unfriend button will be enabled. Before that, users cannot click it. To avoid users clicking the add button many times, I add the disable option after they click it. Now, my problem is that data can be inserted but the unfriend button cannot be clicked. I have already tried a lot of tutorials but haven't fixed the problem. Can anyone help me to solve my problem? Thanks a lot in advance! Below is my code and the form.

<html>
  <script>
    function myFunction(form){
      var subject = prompt("Please enter Subject that want to study");
      var btn = document.getElementById("btn");
      var add = document.getElementById("add");
      btn.disabled=false;
      add.disabled=true;
      if (subject != null){
        form['subject'].value= subject;
        form['add'].value="request sent";
      }
      form.submit();
    }

  function unfriend(form){
    var btn = document.getElementById("btn");
    var add = document.getElementById("add");
    add.disabled=false;
    btn.disabled=true;
    add.value="Add friend"; 
  }
</script>
<body>
  <form method="post" id="form" enctype="multipart/form-data" autocomplete="off"> 
    <input type="hidden" name="id" value="<?php echo $row['register_ID'];?>" />
    <input type="hidden" id="subject" name="subject" data-uid=<?php echo $_SESSION['sid'] ;?>/>
    <td>
      <input type="submit" onclick="myFunction(form);" name="addfriend" data-type='addfriend' id="add" class="btn" value="add" onchange="document.getElementById('btn').disabled = false" />                  
      <input type="submit" value="unfriend" id="btn"  onclick="unfriend(form);" disabled="" />
    </td>
  </form>
</body>
</html>
4

2 Answers 2

1

Disabling a html button by:

document.getElementById("Button").disabled = true;

Enabling a html button by:

document.getElementById("Button").disabled = false;
Sign up to request clarification or add additional context in comments.

Comments

0

The problem with your code is that information is not transmitted between page accesses. So, as you click the button, the page is reloaded and the information lost. You either have to transmit the information (on page load, check what has been send or saved and display that) or do not reload the page.

Another problem is, to use the $_SESSION variable, in PHP you need a session_start() command.

For the second solution, you just do not form.submit() and use type=button instead of type=submit.

Below, an example for the second solution (you can transmit the information to the server on another way, e. g., via AJAX):

<?php session_start(); ?>
<html>
    <head>
        <script>
            function myFunction(form){
                var subject = prompt("Please enter Subject that want to study");
                var btn = document.getElementById("btn");
                var add = document.getElementById("add");
                btn.disabled=false;
                add.disabled=true;
            }
            function unfriend(form){
                var btn = document.getElementById("btn");
                var add = document.getElementById("add");
                add.disabled=false;
                btn.disabled=true;
            }
        </script>
    </head>
    <body>
        <form method="post" id="form" enctype="multipart/form-data" autocomplete="off"> 
            <input type="hidden" name="id" value="<?php echo $row['register_ID'];?>" />
            <input type="hidden" id="subject" name="subject" data-uid="<?php echo $_SESSION['sid'] ;?>" />
            <td>
                <input type="button" onclick="myFunction(form);" name="addfriend" data-type='addfriend' id="add" class="btn" value="add" onchange="document.getElementById('btn').disabled = false" />                  
                <input type="button" value="unfriend" id="btn"  onclick="unfriend(form);" disabled="" />
            </td>
        </form>
    </body>
</html>

Comments

Your Answer

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