John Davidson

php - Printing buttons acording to values on the database

0 comments
Message:


I have 2 buttons, one is to accept, the other one is to refuse and I have a field on the database called "status".

If the value of that field is "Refused" it prints both buttons but the refuse button is disabled, in order to not let the user refuse again, only letting the user accept and if the value is "Accepted" it prints both buttons but the accept one is disabled.

I wanted to know if my code could be improved and if there was a most efficient way to do this than the way that I did it (which I think it's a poor solution and that's why I'm posting this question).

The code:


<?php
if ($status == "Accepted") {
echo "<button disabled style='width:130px;cursor:not-allowed;' class='btn btn-success' name='accept' type='submit'><b>Accept</b></button>";
echo "<button style='width:130px; margin-left: 10px;' class='btn btn-danger' name='refuse' type='submit'><b>Refuse</b></button>";
} else if ($status == "Refused") {
echo "<button disabled style='width:130px;cursor:not-allowed; margin-left: 10px;' class='btn btn-danger' name='refuse' type='submit'><b>Refuse</b></button>";
echo "<button style='width:130px; margin-left: 10px' class='btn btn-success' name='accept' type='submit'><b>Accept</b></button>";
}
?>


My Answer:
status that can have the values "accepted" or "refused". I want to print the buttons according to the value of the status field in the database.

Here is an example code in PHP:

php
// Assume $status is the value retrieved from the database
$status = "accepted"; // or "refused"

if($status == "accepted") {
echo '';
} elseif($status == "refused") {
echo '';
} else {
// Handle other cases if needed
}
?>


In this code, we check the value of the $status variable and print the corresponding button accordingly. You can replace the hardcoded value of $status with the actual value retrieved from the database.

Rate this post

3 of 5 based on 4455 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us