0

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.

dda
  • 6,030
  • 2
  • 25
  • 34
DMSJax
  • 1,709
  • 4
  • 22
  • 35
  • 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 '13 at 03:27
  • What do you even want to do this ? – NullPoiиteя Apr 29 '13 at 03: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]http://stackoverflow.com/questions/8144061/using-php-to-get-dom-element – DMSJax Apr 29 '13 at 03: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 '13 at 03: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 '13 at 03:39
  • @adeneo I'm not seeing an "answer" below - in any case, I realize its wrong and I'm NOT a PHP user normally, so sorry if it seems like I'm missing the obvious. If you want to explain how I should accomplish this or provide the answer you were talking about, I'll review that. – DMSJax Apr 29 '13 at 03:41
  • When the markup is created with PHP, that's when you should be checking for empty values, and just not output the related markup at all. – adeneo Apr 29 '13 at 03:47
  • Where is the markup coming from in the first place? Database? PHP strings? – bfavaretto Apr 29 '13 at 04:01

1 Answers1

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.

  • 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 '13 at 04: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 '13 at 15:49