Link to home
Start Free TrialLog in
Avatar of Mlungisi Ndlela
Mlungisi NdlelaFlag for South Africa

asked on

Display and hide div based on if statement

Hi Guys. I'm having a serious problem that is holding me back. I have a file which is a user dashboard and this file gets data from another php script file which is called by a user login screen when a user clicks login. The file verify the credentials then if correct that its start to pull some data from the database. The problem is that now there are some users who don't complete their registration process which some stop where they need to make their payments so their accounts are kept with a status of pending which indicate that the user has not pay the registration fee. Now I am able to read the status from the database and assign it to the session so that I will work with on the dashboard, this controls which tells if the complete your registration button should be shown or not of which in this case because the status is pending the dashboard should show this button. My problem is showing or hiding it as this should be hidden when a user has paid his/her registration fee because this update the database with Active status.

I've tried this:
<div if($_SESSION['iiio'] == "pending"){echo " style='display: block';"};><a href="https://www.paymentgateway.com" ><button align="right" id="completeReg" name="completeReg"  class="btn btn-danger my-cart-btn my-cart-b" >Complete RegistrationComplete Registration</button></a></div>

Open in new window


But the above will just do what the `display:xxxx` has if it has `none` it won't display the block even if the condition is not met for this where it should display because the condition is no met and if I had `block` it will keep displaying the even if the condition is not met. I've also tried adding the `else` but still.

I've also tried JavaScript:
<script type="text/javascript">

     var xadminFee = '<%= Session["iiio"] %>';
     if(xadminFee == "pending"){
        
         document.getElementById("completeReg").style.display = "block"; // This is for displaying the complete registration process.
     }else{
        document.getElementById("completeReg").style.display = "none";
}

  </script>

Open in new window


And still this seems as if its doesn't work at all because the div will always be shown even if I put `none`n but still.

Remember the session is from a php file which contains the exact status from the database.

Anyone who can help me solve this problem.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Just wrap your entire button inside a PHP if statement:

<?php if($_SESSION['iiio'] == "pending"): ?>

<div>
    <a href="https://www.paymentgateway.com">
        <button align="right" id="completeReg" name="completeReg" class="btn btn-danger my-cart-btn my-cart-b" >Complete Registration</button>
    </a>
</div>

<?php endif; ?>

Open in new window

Now that block will only be rendered if iiio is 'pending'
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.