1

I spent all morning researching and, with the help of SO, figured out how to grab the innerHTML of elements using Javascript.

This is to populate meta tags for search and social open graph. The tag in question here is the meta description tag.

What I used in Javascript was this:

document.getElementsByClassName("report-description-text")[0].childNodes[2].nodeValue;

The (somewhat awkward) html structure is this:

<!-- start report description -->
<div class="report-description-text">
<h5>Description</h5>
Welcome to Ushahidi. Please replace this report with a valid incident
<br/>
</div>

I want to grab the text "Welcome to Ushahidi...". But this text is not inside any html tags, it's just text. The above JS works in grabbing that text.

I then tried to integrate this into a PHP script but was swiftly downvoted since, I now know, that you cannot execute JS on the server side (I knew that but never had enough practice to really get it).

The PHP script looks like this:

if( strpos($url, '/reports/view') === 0 ) { 
echo '<!-- Make this site discoverable, shareable and searchable -->
<meta name="description" content=[figure out what to put here]/>
// deal with these other metas later                    
<meta name="author" content="my name" />

I'd like the content of the content to be, in this example: content="Welcome to Ushahidi...." />

How would I do that using PHP? Is it as straightforward as with JS? I did some research but just worked myself into confusion.

6
  • Look into the DOMDocument class, which is a DOM parser in PHP. php.net/manual/en/class.domdocument.php. Commented Apr 12, 2014 at 15:12
  • php is run before the html.So that text is known to php. so, what are you trying to do again? Commented Apr 12, 2014 at 15:23
  • @Aris I want to output a metatag in the header like so: <meta name="Description" content="Welcome to Ushahidi..." /> This is for all the pages of this nature. The description in the div element above should be grabbed and used to populate the meta tags Commented Apr 12, 2014 at 15:31
  • How is that DIV element being generated? If it's something recurring you can use a variable on the HTML page, for example $topDIV; this variable can then be echoed in the metatag aswell. (basically PHP knows of the text before it's being sent to the browser, PHP serves it(using includes normally), so it's all about varaibles. Commented Apr 12, 2014 at 15:36
  • @Patrick yes! Of course. The best answers are the obvious ones and I never considered this (I don;t have much experience). Next step will be to research which variable is being spat out here. Will research the platforms documentation (wiki.ushahidi.com/display/WIKI/Ushahidi+v2.X+Developer+Guide) Commented Apr 12, 2014 at 15:38

1 Answer 1

1

You must understand the PHP is run on the server side first, and then the Html is rendered in client side.

You should try to face the problem the other way around. Try something along the following lines:

In PHP define :

$description = "Welcome to Ushahidi. Please replace this report with a valid incident";

You can use this variable in all you PHP code:

echo '<!-- Make this site discoverable, shareable and searchable -->
<meta name="description" content=['.$description.']/>
// deal with these other metas later                    
<meta name="author" content="my name" />

Then on the html, you can still use this variable, since it is already set:

<h5>Description</h5>
<?php echo $description; ?>
<br/>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer @Aris I'm going to do some research on the back of Patricks comment. I'm using a platform and there must be a variable already set that outputs the description. The description is stored int he database for each report page (The page in question is a report page). Therefore there must be a preexisting variable that outputs this. There is not magic tool to see what variable is being passed here is there? For example I' a data analyst and use HTTPfox often to see what Google Analytics parameters are being passed. Separate question I guess
my answer and Patricks comment say the same thing.No, you cannot see the php parameters, unless you have access to the code.
I have access to the code in that it's my website and I have ftp access. Is there a conventional way or do folk typically trial and error with echo?
This is a good answer, and as Aris stated, you must have access to the actual code to see what's being parsed.
you just need to find the related file and see how the value is displayed. Even if it's plain html, you can always change it to a PHP variable.

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.