user2371301
user2371301

Reputation: 3474

Hide/Show Input's based on mysql data

I have an active session in my page using: $_session_start(); I want to hide part of my form and show another based on the users previous entries.

Basically I offer Direct Deposit and Paper Check I want to be able to have the user only see the fields required for them to complete previously (in signup so the values are already in the database)

Right now the database table is set up like this:

paper_check with a value of 1 (yes pay this way) or 0 (no pay this way)

and the same thing with direct deposit. I need a way to show/hide the fields associated with each based on the users previous selections.

I typed this up but it doesn't seem to do anything. Please help me!!!!! (The class names hide and show are in my css and working properly!)

<?php 
    if ($_SESSION['direct_deposit'] == 1)
    {
      $doc->getElementById('check_payable_to')->className+" hide";
    }
    else
    {
      $doc->getElementById('name_on_account')->className+" hide";
      $doc->getElementById('check_payable_to')->className+" show";
    } 
?>

Upvotes: 1

Views: 508

Answers (4)

user2371301
user2371301

Reputation: 3474

By wrapping each section in it's own div like so:

<div id="paperCheck class="show"> 
   <!--Your Paper Check Information here-->
</div> 

<div id="directDeposit" class="show"> 
   <!--Your Direct Deposit Information here--> 
</div> 

And then by using javascript you are able to do the following:

var direct_deposit = <?php echo $_SESSION['direct_deposit']; ?>;

if (direct_deposit == 1) 
{
   document.getElementById('paperCheck').className ="hide";
} 
else 
{
   document.getElementById('directDeposit').className ="hide";
}

Upvotes: 0

Barmar
Barmar

Reputation: 781058

I don't see a className property in the PHP DOM library. It looks like this is the way to add a class to an element:

$doc->getElementById('check_payable_to')->setAttribute('class',
   $doc->getElementById('check_payable_to')->getAttribute('class') . " hide");

Do you have error reporting enabled? It should be warning about the unknown property.

If you want to do this in Javascript instead of PHP, have your PHP do something like:

<script>
var direct_deposit = <?php echo $_SESSION['direct_deposit']; ?>;
if (direct_deposit == 1) {
   /* Do what you want */
} else {
   /* Do what else you want */
}
</script>

Upvotes: 1

className+" hide" should be className .= " hide"

Upvotes: 0

Choudhury A. M.
Choudhury A. M.

Reputation: 5202

In your question you said "I have an active session in my page using: $_session_start();" You shouldn't use $_session_start(). It should be session_start() .

Upvotes: 0

Related Questions