Home > Software design >  Href for button PHP HTML
Href for button PHP HTML

Time:11-07

Hi i want to href after user click a button

This is my current code but it's not working. After I click, the confirmation will come out but it doesn't go to PHadmin_approveHospital.php after I click on.

echo "<a href=\"PHadmin_approveHospital.php?id=".$row["HospitalID"]."\" onclick=\"return  confirm('do you want to approve Y/N')\"><input type=\"submit\" value=\"Approve\"></a>";

Full Function

public function displayAllHospital() {
        $sql = "SELECT * FROM hospital";
        $result = @mysqli_query($this->conn, $sql);
        echo "<table class='table table-bordered'>";
        echo "<thead>";
        echo "<tr>";
        echo "<th>ID # <i class='fa fa-sort'></i></th>";
        echo "<th>Name </th>";
        echo "<th>Email </th>";
        echo "<th>Contact Number <i class='fa fa-sort'></i></th>";
        echo "<th>Status </th>";
        echo "<th>Actions</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";
        while($row = mysqli_fetch_assoc($result)){
            echo "<tr>";
            echo "<td>" . $row["HospitalID"] . "</td>";
            echo "<td>" . $row["Hospitalname"] . "</td>" ;
            echo "<td>" . $row["email"] . "</td>" ;
            echo "<td>" . $row["contactno"] . "</td>" ;
            echo "<td>" . $row["status"] . "</td>" ;
            echo "<td>";
            echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='view' title='View' data-toggle='tooltip'><i class='material-icons'>&#xE417;</i></a>";
            echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='edit' title='Edit' data-toggle='tooltip'><i class='material-icons'>&#xE254;</i></a>";
            echo "<a href=\"PHadmin_deleteHospital.php?id=".$row["HospitalID"]."\" onclick=\"return  confirm('do you want to delete Y/N')\" class='delete' title='Delete' data-toggle='tooltip'><i class='material-icons'>&#xE872;</i></a>";
            echo "</td>";
            echo "<td>";
            if($row["status"] == "pending"){
                echo "<a href=\"PHadmin_approveHospital.php?id=".$row["HospitalID"]."\" onclick=\"return  confirm('do you want to approve Y/N')\"><input type=\"submit\" value=\"Approve\"></a>";
            }
            echo "</td>";
            echo "</tr>";
            echo "</tbody>"; 
            echo "</form>";
            echo "</tr>";
        }
        echo "</table>";
    }

Thanks for helping.

CodePudding user response:

You may call a javascript function when you click the button, and then display the confirmation message, so that the user can confirm or not (if confirmed, jump to the url)

You may use the following code :

<input type="button" value='Approve' onclick="javascript:check1();">

<script>
function check1() {
   if(confirm("Do you want to approve?"))  {
      window.location.href="PHadmin_approveHospital.php?id=<?php echo $row["HospitalID"]. '\'; ?>";
   }    
}
</script>

So , for your updated code, it will be:

<?php

public function displayAllHospital() {


echo '
<script> 
function check1(var1) {
   if(confirm("Sure to delete ?"))  {
      window.location.href="PHadmin_deleteHospital.php?id="  var1;
   }    
}

function check2(var2) {
   if(confirm("Sure to approve ?"))  {
      window.location.href="PHadmin_approveHospital.php?id="   var2;
   }    
}
</script>';




        $sql = "SELECT * FROM hospital";
        $result = @mysqli_query($this->conn, $sql);
        echo "<table class='table table-bordered'>";
        echo "<thead>";
        echo "<tr>";
        echo "<th>ID # <i class='fa fa-sort'></i></th>";
        echo "<th>Name </th>";
        echo "<th>Email </th>";
        echo "<th>Contact Number <i class='fa fa-sort'></i></th>";
        echo "<th>Status </th>";
        echo "<th>Actions</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";
        while($row = mysqli_fetch_assoc($result)){
            echo "<tr>";
            echo "<td>" . $row["HospitalID"] . "</td>";
            echo "<td>" . $row["Hospitalname"] . "</td>" ;
            echo "<td>" . $row["email"] . "</td>" ;
            echo "<td>" . $row["contactno"] . "</td>" ;
            echo "<td>" . $row["status"] . "</td>" ;
            echo "<td>";
            echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='view' title='View' data-toggle='tooltip'><i class='material-icons'>&#xE417;</i></a>";
            echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='edit' title='Edit' data-toggle='tooltip'><i class='material-icons'>&#xE254;</i></a>";
            echo "<input type=button value=Delete onclick='javascript:check1(". $row["HospitalID"]  . ")';>";

            echo "</td>";
            echo "<td>";
            if($row["status"] == "pending"){

            echo "<input type=button value=Approve onclick='javascript:check2(". $row["HospitalID"]  . ")';>";

            }

            echo "</td>";
            echo "</tr>";
            echo "</tbody>"; 
            echo "</form>";
            echo "</tr>";
        }
        echo "</table>";
    }


    ?>
  • Related