0

After the user submits a form the .php file is supposed to display only certain parts of the forum depending on if cc_owner has a value of personal or something else. I have the contents to be displayed in two separate div elements one called CreditContent_ID and the other called BillContent_ID the inline css sets both div display elements to 'none'. I am trying to use php to check whether the value of cc_owner is personal or something else. If it's personal I want it to change the CreditContent_ID display to block.

I think the problem is with this line:

echo 'document.getElementById("CreditContent_ID").style.display="block"'; 

Here is my code:

<style>
    .CreditContent{
        display:none;

    }
    .BillContent{
        display:none;
    }
</style>
<body>
    <?php 
        if($_POST['cc_owner']=='personal'){
            echo '<script language="javascript">';
            echo 'document.getElementById("CreditContent_ID").style.display="block"'; 
            echo '</script>';
        }else{
            echo '<script language="javascript">';
            echo 'document.getElementById("BillContent_ID").style.display="block"';
            echo '</script>';

        }
    ?>
    <div class=CreditContent id="CreditContent_ID">
        //I have other code here.
    </div>
    <div class=BillContent id="BillContent_ID">
        //more code here
    </div>
</body>
4
  • What is the issue? is there any error? Commented Nov 21, 2019 at 17:16
  • 3
    instead of updating CSS using javascript why can't you update CSS using PHP? Commented Nov 21, 2019 at 17:18
  • ^^ Afterall you are doing a submit round trip to the PHP code on the server Commented Nov 21, 2019 at 17:19
  • Why not just put <div class=CreditContent id="CreditContent_ID"> inside if and <div class=BillContent id="BillContent_ID"> inside else? (would be nice to save this in cookie or something wo it will work after page reload) Commented Nov 22, 2019 at 7:48

1 Answer 1

1
<style>
.hideContent {
    display: none;
}
</style>

<body>
    <div class="CreditContent <?php echo $_POST['cc_owner'] != 'personal'? 'hideContent': ''; ?>" 
        id="CreditContent_ID">
        //I have other code here.
    </div>

    <div class="BillContent <?php echo $_POST['cc_owner'] == 'personal'? 'hideContent': ''; ?>" 
        id="BillContent_ID">
        //more code here
    </div>
</body>

Add css class hideContent to div based on cc_owner value.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried it, and it wasn't working for me. But I was able to make a few minor changes to get it working. Thanks for your help! This is what I changed. <div class="<?php if($_POST['cc_owner'] == 'personal'){echo 'hideContent';} ?>" id="BillContent_ID">
@Kman don't forget to mark it as solution. However I would change the php-injection this way: <?= $_POST['cc_owner'] === 'personal' ? 'hideContent' : '' ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.