-1

I have a HTML template with DIVs holding schema.org markup. The DIV content is populated via PHP but some DIVs are left blank/empty when we lack the data to put in them. Currently, we can hide the DIV by checking a field length using the javascript below - BUT, because it is a client-side, Google's rich data testing tool still sees the schema.org markup in the code with missing/empty content and complains about missing data.

So, I need the DIV hidden if empty, server-side before processing. I can't figure out what the PHP equivalent would be to this Javascript check.

JAVASCRIPT METHOD IN USE

<script>
var inside = document.getElementById("persona");
if(inside.innerHTML.length < 1)    {
        document.getElementById("staffm").style.display = "none";
    }    else    {
        document.getElementById("staffm").style.display = "block";
    }
</script>

THE ABOMINABLE PHP CODE I ATTEMPTED TO USE (VERSION 3 BILLION SOMETHING)

<?php
  $div = getElementById('staffm');
  if (ob_get_length($div) < 1) {
    $div->setAttribute("style", "display:none");
  } else {
    die('');
  }
?>

I don't think this is nowhere near correct but I honestly can't find a "simple" resource/post about this kind of task - the other questions/answers seem to get deeper than I need or understand and I'm getting lost. So, any help would be appreciated.

8
  • 3
    What the heck is this? You don't have access to the DOM in PHP, and the languages are completely different, used in different places etc. so there is no substituting javascript for PHP.
    – adeneo
    Apr 29, 2013 at 3:27
  • What do you even want to do this ? Apr 29, 2013 at 3:30
  • @adeno Yes you can access the DOM in PHP but admittedly the method being used is a bit above my understand of PHP, here is one example [link]stackoverflow.com/questions/8144061/…
    – DMSJax
    Apr 29, 2013 at 3:35
  • 2
    Parsing HTML by creating a DOMDocument is not the same as accessing the actual DOM in the clients browser the way that javascript does. I think you're not really getting the concept of this, and should probably take the advice in the answer below to read up on the essentials.
    – adeneo
    Apr 29, 2013 at 3:37
  • @NullPointer - I'm not sure how to explain it any clearer - If the DIV's with schema.org markup are null/empty I need the CSS style attribute of DIV set to display:none; so that the empty markup isn't attempted to be read/parsed by rich data processor (Google's specifically)
    – DMSJax
    Apr 29, 2013 at 3:39

1 Answer 1

4

Using of "getElementById" in PHP is invalid, because PHP is executed in the server, not in the client, so, you are trying to access to a html object that is on the client web browser with a server language (PHP). I recommend you learn about DOM manipulation and Ajax to "comunicate" Javascript and PHP with an XMLHttpRequest object.

2
  • 1
    For the sake of pedantry, getElementById is not a native PHP global function. There exists countless userland implementations for DOM traversal/modification (PHPQuery comes to mind) in which such functions very may well exist. OP might be best to investigate something like this.
    – Dan Lugg
    Apr 29, 2013 at 4:09
  • After the initial comments above and some more research i simply abandoned that approach entirely and found a different route to reach the result needed. - So problem solved in any case. Thank you to all for the input constructive or otherwise.
    – DMSJax
    Apr 29, 2013 at 15:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.