0

Hiii everyone,

I just want to show one div if the database value is equal to some value.I tried like this

 <?php echo $show['birth_certificate_available']=='Available') { ?>
        <script type="text/javascript">
       $(".birth").show();
        </script>
        <?php } ?>

But the div is not showing.I dont know why its not working.Kindly guide me to correct this issue.

5
  • If the value is not equal to certain value than I am assuming that the div should be hidden, is this the case? Commented Dec 13, 2016 at 9:48
  • Begin by writing debug information about $show. You need to know first if it goes through your condition, so just use var_dump($show) and write down below your code snippet what it displayed on your screen. Commented Dec 13, 2016 at 9:49
  • can you read that php statement in plain english? Commented Dec 13, 2016 at 10:26
  • is this issue solved? (maybe you forget to mark an answer as solving your question) or the missing jQuery library was not the issue? Commented Dec 13, 2016 at 10:37
  • Im asking in general added JQuery library in page.Then why I want to add sepertely in if statemnt inside If i call the library multiple time my bandwidth makes prblm right Commented Dec 13, 2016 at 10:39

4 Answers 4

1

Please modify the code accordingly :

<?php  if($show['birth_certificate_available']=='Available') { // put a condition?>
        <script type="text/javascript">
        $(document).ready(function(){ // jQuery ready function
              $(".birth").show();
        });
        </script>
<?php } ?>
Sign up to request clarification or add additional context in comments.

5 Comments

'birth_certificate_available' => string 'Available' (length=9).. This is what im getting..
you may want to avoid adding a comment right before to close the PHP tag as in // put a condition?> because it can confuse wheter or not the ?> tag is executed
@Alexandre it's just a comment.. i put it on for clarification
@KavyaShree, try to echo some string inside if condition just to know whether we are on right way or not
Ya I tried with alert inside instead of show hide.Thats working fine
0

Wrap it in ready() function as follows,

Assuming you have already imported the relevant jquery lib in your code.

<?php echo $show['birth_certificate_available']=='Available') { ?>
        <script type="text/javascript">
          $(document).ready(function(){
             $(".birth").show();
          });
        </script>
<?php } ?>

Comments

0

I missed JQuery reference document so that its not worked.Now I added script document its working.Thanks all who helped me to fix this.

Comments

0

Solution

echo outputs data to the page. To write a condition, you would like to use if statements.

<?php if ($show['birth_certificate_available'] === 'Available'): ?>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $('.birth').show();
        });
    </script>
<?php endif; ?>

Note: if jQuery is already linked to your document, remove the <script src="..."></script> line. Otherwise, I suggest you to move the <script src="..."></script> line right before the </body> HTML closing tag to speedup HTML elements loading:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <div class="birth" hidden="hidden">Foobar</div>
        <?php if ($show['birth_certificate_available'] === 'Available'): ?>
            <script>
                $(document).ready(function() {
                    $('.birth').show();
                });
            </script>
        <?php endif; ?>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    </body>
</html>

Note: I use the === equal assertion to force type testing. Whatever, be careful: testing is case-sensitive, whether it is == or ===

The div element must have the birth class, must exist and must be hidden in order to successfully show() it

Debug

$show variable

If this is not working, and in order to debug the $show variable, you may want to use var_dump() as so:

var_dump($show);

and ensure it is an array and has the birth_certificate_available index defined.

Javascript errors

Use the browser console to retrieve javascript errors (such as inexisting div elements...)

9 Comments

I tried this too but div not showing. I used var_dump it shows value correctly.
You defined the <div /> element with the class birth as something like this: <div class="birth">? Does the <div /> element hidden? Does it exist at the page creation?
No I defined with <div class="birth">
If you search in the source code from your browser, is the div element existing? Has it the hidden="hidden" attribute and value? (or equivalent)
No @Alex. I searched tht but nothing is like that in browser.
|

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.