PHP change button text and update database accordingly

How my function works:

There are 2 buttons ‘Accept’ and ‘Reject’. When user clicks on Accept, the text will change to ‘Accepted’ and both buttons will be disabled, the same thing with Reject button too. After this process, I need to update the database table column accordingly.

Currently, I was able to update the database according to the button but button text not changed. These are my codes;


 $counter = 1;
 $queryst = mysqli_query($conn,"SELECT * FROM applicants WHERE admission_status 
  = 0 ORDER BY registered_date DESC");

        echo '<table class="table table-striped" id="tblData">';
        echo "<thead>";
        echo "<tr>";
        echo "<th>NO</th>";
       echo "<th>ID</th>";
        echo "<th>NAME</th>";
        echo "<th>STATUS</th>";
        echo "<tr>";
        echo "</thead>";
        echo "<tbody>";
      while($rowst = mysqli_fetch_assoc($queryst))
       echo "<tr>";
       echo "<td>".$counter. "</td>";
       echo "<td>" .$rowst['application_no']."'>".$rowst['application_no']."</a></td>";
        echo "<td>" . $rowst['surname'] . " ".$rowst['firstname']. "</td>";
        echo "<td>      
     <a href='appr.php?id=".$rowst['application_no']."'> <input type='button' value='accept' 
      id='accept' name='accept' class='toggle btn btn-success'> </a></td>";
         echo "<td> <a href='disappr.php?id=".$rowst['application_no']."'>  <input type='button' 
     value='Reject' name='reject' class='toggle btn btn-danger'> </a></td>";
         echo "</tr>";
        echo "</tbody>";
        echo "</table>";

The code above is my html code.
This is the php code to accept the candidate with id number.

      $getid= $_GET["id"];

      $sqla = "UPDATE applicants SET admission_status = 1 WHERE application_no = 
     '".$getid."' ";
     $result = mysqli_query($conn, $sqla);
    echo "Operation failed";

My database was updated but the button was not changed.
I am new to programming, I just want that button text to change value when click and thereafter disable both button.
Please help me

Well, this is a complicated one for a newbie, but, let’s give it a try.

First, to disable a button, you add DISABLED to the button itself. Normally, you use “Disabled=disabled” to make sure the browser sees the option has a value. Now to do this in a script, you need to do it in the HTML itself as that is where this goes. Also, you need a compare of sorts to indicate the value change.

In your case, you have a status for this set up as “admission_status”. To alter buttons in the HTML using this value, you would need to use this value to change the TEXT in the button itself to add the DISABLED text itself. Here are some possibles on this:

    $disabled_text = $rowst['admission_status'] = 1 ? "disabled='disabled'" : "";>

    <input type='button' value='accept' id='accept' name='accept' class='toggle btn btn-success' <?PHP echo $disabled_text; ?>>

This is just for one test and one button. Just an example. The first line would be placed after the current applicant is displayed. It tests to see if the status is equal to 1 which means not accepted/rejected and makes the text either empty or disabled. The section like inserts that value (blank or disabled) into the button itself. This turns off or on the disabled option in the button. Make sense?

Now, you have one problem with your code. You select only the open applicants by the WHERE clause looking for a status of zero. So, only these would show anyways. You would never need to do this code to show others since the button would always need to show if the status is zero. Make sense?

I think your want to have at least three values for the status. 0=open, 1=accepted, 2=rejected and then handle the buttons accordingly.

Hope this helps to get you started. Good luck!

Thanks. I will try it now and get back to you

Thanks EmieAlex, I have solved it now. Your suggestion really help in solving this problem

You are welcome! Glad you solved it. As I have said on other threads, it is nice to solve a programming puzzle! See you in the next “puzzle”…

Sponsor our Newsletter | Privacy Policy | Terms of Service