0

How my function works:

There are 2 buttons 'Accept' and 'Decline'. When user clicks on Accept, the text will change to 'Accepted' and both buttons will be disabled. Colour of text changes too. After this process, i need to update the database table column accordingly.

My current situation:

I'm showing more than one entry with this function. Currently my codes only work when i have one entry and it doesn't stays the way it should be when I pressed on it after I refresh the page. When there are more than one entry, the button i clicked on the second entry somehow detects the first entry and changes the button in the first entry. And i have no clue on how to update the database accordingly.

Thank You so much in advance.

My Script:

<script>
        function accept() {
            document.getElementById("accept").disabled = true;
            document.getElementById("decline").disabled = true;
            document.getElementById("accept").innerHTML = 'Accepted';
            document.getElementById("accept").style.color = "green";

        }

        function decline() {
            document.getElementById("decline").disabled = true;
            document.getElementById("accept").disabled = true;
            document.getElementById("decline").innerHTML = 'Declined';
            document.getElementById("decline").style.color = "red";
        }
    </script>

Accept Button:

<button id="accept" onclick="accept()">Accept</button>

Decline Button:

<button id="decline" onclick="decline()">Decline</button>
1
  • use js between window.onload=function(){} Commented Nov 8, 2015 at 4:52

2 Answers 2

1

You need to learn how to use AJAX:
Using JQuery AJAX makes it much easier.
JS FILE:

$.ajax({
    url: "/updateDatabase.php";
    type: "POST";
    data: {update: value},
    beforeSend: function (){
       //stuff you like to do before sending.
    }
    success: function (data){
       //do something with return data
    }

});

PHP FILE: updateDatabase.php

$var = $_POST["update"]; //make sure this is the same name for the data{} json string
//update database.

echo "Put return value here for JS success data var."

Remember Button state:

<?php if(databaseValue == Accepted) { ?>
      <button>Format Button Disabled for accepted</button>

<?php } else { ?>
      <button>Format button for enabled</button>

<?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Why are you writing two functions if 50% of the operations of the functions are same. I suggest you to write only one function and put a conditional statement in it which checks which button was clicked. It is efficient programming!

Regarding your question can you share a screenshot here, as what exactly is displayed when the operation is performed.

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.