fantasiaa123
fantasiaa123

Reputation: 25

make button disabled based on if row column

I have some working PHP code and I have recently added a button that allows the user to download the image form the root directory, in the database we put the file name e.g. example.png / example.jpeg and when the user clicks download it opens the image in a new tab

what we need is: if the [proof] is= NULL , the download button disables, otherwise it will be enabled and they can click the button

 <?php 
// output data of each row
while($row=mysqli_fetch_assoc($designresult)) {

?>

<div class="card mb-4 box-shadow"><div class="card-header">
    <h4 class="my-0 font-weight-normal">Job Reference: <?php echo $row["jobRef"]; ?></h4>
</div>
<div class="card-body">
    <p><b>Company Name:</b><br> <?php echo $row["companyName"]; ?> </p>
     <p><b>Requested:</b><br> <?php echo $row["dateReq"]; ?> </p>
     <p><b>Request By:</b><br> <?php echo $row["yourName"]; ?> </p>
     <p><b>Graphic Type:</b><br> <?php echo $row["graphicType"]; ?> </p>
     <p><b>Double Sided:</b><br> <?php echo $row["doubleS"]; ?> </p>
     <p><b>Design Info:</b><br> <?php echo $row["info"]; ?> </p>
     <p><b>Purpose:</b><br> <?php echo $row["purpose"]; ?> </p>
     <p><b>Proof:</b><br> <?php echo $row["approved"]; ?> </p>
     <p><b>Proof Date:</b><br> <?php echo $row["appDate"]; ?> </p>

   <a class="btn btn-success" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>">Download</a>

</div>
</div>

<?php

}
?>

Upvotes: 0

Views: 323

Answers (3)

Dum
Dum

Reputation: 1501

<?php 
$state = (empty($row['proof'])) ? "disabled='disabled'" : ""; 
$class = (empty($row['proof'])) ? "disabled" : "";
?>
<a class="btn btn-success <?php echo $class; ?>" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>" <?php echo $state; ?>>Download</a>

To disabled the button, you need to use disabled HTML attribute. The code above checks $row['proof'] == NULL. If this statement is true it prints disabled = "disabled" in the button element and it isn't true, it prints nothing.

Assuming that you are using bootstrap, .disabled will grayed out the button.

Upvotes: 0

BlindPenguin
BlindPenguin

Reputation: 116

To my knowledge the link tag does not have a "disabled" attribute. But you can "disable" the link by removing the href attribute.

Something like this: It checks if $row['proof'] has some value (by negating the empty), then it prints out the href if result is true.

<a class="btn btn-success" target="_blank" <?php if(!empty($row['proof'])): ?> href="<?php echo IMAGE_DIR . $row['proof']; ?>" <?php endif; ?> >Download</a>

Or maybe better: Check if variable is empty and give the user a hint that it's not available. I think this is the better solution, because then your users will know what's going on.

<?php if(empty($row['proof'])): ?>
    <span>No proof available</span>
<?php else: ?>
    <a class="btn btn-success" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>">
        Download
    </a>
<?php endif; ?>

Upvotes: 1

Rudra
Rudra

Reputation: 714

<a
class="btn btn-success" 
target="_blank"
<?php if(empty($row['proof'])) echo "disabled"; ?>
href="<?php if(!empty($row['proof'])) echo IMAGE_DIR . $row['proof']; ?>">
Download
</a>

try above code. and add disabled class using this condition

Upvotes: 0

Related Questions