Create DOM elements using Javascript - php

Hi I am trying to retrieve RSS feed using PHP and javascript.
I can get RSS feeds by using PHP but I want to create DOM elements for RSS feed using javascript. I am not sure how to complete this. Could anyone share some tips? Thanks a lot.
PHP
function parse_rss_feed($url){
$contents= file_get_contents($url);
$xmlStr= simplexml_load_string($contents);
return $xmlStr;
}
function get_rss_feed($xmlStr){
echo '<ul>';
foreach ($xmlStr->item as $node):
//I want to transfer my RSS value to my javascript....
$title=$node->title;
$author=$node->creator;
$desc=$node->description;
endforeach;
echo '</ul>';
}
html
<head>
<script type="text/javascript" src="js/slideshow.js"></script>
</head>
<body>
<?php
include 'getFeed.php';
parse_rss_feed('http://myRSSFeed')
get_rss_feed($xmlStr)
?>
</body>
slideshow.js
var rss=document.createElement('artical');
//How do I get the value from my php...
rss.innerHtml=.......???

Couldn't be easier, createElement().
var el = document.createElement('div')
Edit:
To mix JavaScript with PHP just escape it as you'd usually do but I suggest if you're doing something serious to learn about AJAX.
?>
<script>
var foo = <?php echo 'baz' ?>
</script>
<?php

Related

php, html and pdf with drupal

Having the next PHP code that produce HTML code:
(simplified function, the real one on same idea but longer with loops and so on):
<?php
function show_doc_html() {
$text_to_title = "some text from db";
?>
<html>
<head>
<title>
<?php echo $text_to_title ?>
</title>
</head>
<body>
</body>
</html>
<?
}
I would like to return a PDF to the user, without changing too much of this code. we are working under drupal so we have function that can get html string and convert it to pdf, but the former function doesn't return anything but printing to stdout. Id it possible? or should i rebuild the old function to return string?
Is that what you need?
I have used it in my project. You can just write your markup and inline css in node template using view_mode = 'PDF'
The easiest way was to encapsulate my function with "ob_start()" get all text using "ob_get_contents()", then convert it to pdf.
Something like:
function show_doc_pdf() {
ob_start();
function show_doc_html() ;
$html_var = ob_get_contents();
ob_end_clean();
//use wkhtmltopdf api on $html_var to return pdf
}

How to code PHP function that displays a specific div from external file if div called by getElementById has no value?

Thank you for answering my question so quickly. I did some more digging and ultimately found a solution for grabbing data from external file and specific div and posting it into another document using PHP DOMDocument. Now I'm looking to improve the code by adding an if condition that will grab data from a different div if the one called for initially by getElementById has now data. Here is the code for what I got so far.
External html as source.
<div id="tab1_header" class="cushycms"><h2>Meeting - 12:00pm to 3:00pm</h2></div>
My PHP file calling from source looks like this.
<?php
$source = "user_data.htm";
$dom = new DOMDocument();
$dom->loadHTMLFile($source);
$dom->preserveWhiteSpace = false;
$tab1_header = $dom->getElementById('tab1_header');
?>
<html>
<head>
<title></title>
</head>
<body>
<div><h2><?php echo $tab1_header->nodeValue; ?></h2></div>
</body>
</html>
The following function will output a message if a div id can't be found but...
if(!tab1_header)
{
die("Element not found");
}
I would like to call for a different div if the one called for initially has no data. Meaning if <div id="tab1_header"></div> then grab <div id="alternate"><img src="filler.png" /></div>. Can someone help me modify the function above to achieve this result.
Thanks.
either split up master.php so div1\2 are in a file each or set them each to a var, them include master.php, and use the appropriate variable
master.php
$d1='<div id="description1">Some Text</div>';
$d2='<div id="description2">Some Text</div>';
description1.php
include 'master.php';
echo $d1;
You can't do this solely with PHP includes unless you put the divs into separate files. Look into PHP templating; it's probably the best solution for this. Or, since you're new to the language, try using variables:
master.php
$description1 = '<div id="description1">Some Text</div>';
$description2 = '<div id="description2">Some Text</div>';
board1.php
include 'master.php';
echo $description1;
board2.php
include 'master.php';
echo $description2;
Alternatively, you could use JavaScript, but that might get a little messy.
Short answer is: although it's possible it's probably very bad idea taking this approach.
Longer answer: the solution may turn out to be too complicated. If in your master.php file is only HTML markup, you could read content of that file with file_get_contents() function and then parse it (i.e. with DOMDocument library functions). You would have to look for a div with given id.
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$divs = $doc->getElementsByTagName('div');
foreach ($divs as $div)
{
if( $div->getAttribute('id') == 'description1' )
{
echo $div->nodeValue."\n";
}
}
?>
If your master.php file has also some dynamic content you could do following trick:
<?php
ob_start();
include('master.php');
$sMasterPhpContent = ob_get_clean();
// same as above - parse HTML
?>
Edit:
$tab_header = $dom->getElementById('tab1_header') ? $dom->getElementById('tab1_header') : $dom->getElementById('tab2_header');

How to open a link which was parsed in a div?

My site parses a spanish dictionary and lets you search for more than one word at a time. If you look up "hola" you get the first div). Some words come up with suggestions, like "casa", instead of definitions like "hola":
And what i am looking for is this:
So, i would like to: when you click on the suggestions (like CASAR in the example I posted) to print the result in a div like HOLA. Here is the code used:
$words = array('word0','word-1');
function url_decode($string){
return urldecode(utf8_decode($string));
}
$baseUrl = 'http://lema.rae.es/drae/srv/search?val=';
$cssReplace = <<<EOT
<style type="text/css">
// I changed the style
</style>
</head>
EOT;
$resultIndex = 0;
foreach($words as $word) {
if(!isset($_REQUEST[$word]))
continue;
$contents = file_get_contents($baseUrl . urldecode(utf8_decode($_REQUEST[$word])));
$contents = str_replace('</head>', $cssReplace, $contents);
$contents = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/search', $contents);
echo "<div style='
//style
", (++$resultIndex) ,"'>", $contents,
"</div>";
}
I have tried with: $contents .= '' . $word . '<br/>'; But it didn't work nor I know really where/how to use it.
Okay, I'll use jQuery for the example because it will be the easiest to get the job done specially if you are new to programming.
NOTE: I DO NOT RECOMMEND USING JQUERY -BEFORE- LEARNING JAVASCRIPT -- DO IT AT YOUR OWN RISK, BUT AT LEAST COME BACK AND LEARN JAVASCRIPT LATER
First, read up on how to download and install jquery here.
Secondly, you will want something a little like this, let's pretend this is your markup.
<div id="wrapper">
<!-- This output div will contain the output of whatever the MAIN
word being displayed is, this is where HOLA would be from your first example -->
<div id="output">
</div>
<!-- This is your suggestions box, assuming all anchor tags in here will result in
a new word being displayed in output -->
<div id="suggestions">
</div>
</div>
<!-- Le javascript -->
<script>
// Standard jQuery stuff, read about it on the jquery documentation
$(function() {
// The below line is a selector, it will select any anchor tag inside the div with 'suggestions' as identifier
$('#suggestions a').click(function(e) {
// First, stop the link going anywhere
e.preventDefault();
// Secondly, we want to replace the content from output with new content, we will use AJAX here
// which you should also read about, basically we set a php page, and send a request to it
// and display the result without refreshing your page
$.ajax({
url: 'phppage.php',
data: { word: 'casar' },
success: function(output) {
// This code here will replace the contents of the output div with the output we brought back from your php page
$('#output').html(output);
}
})
});
})
</script>
Hopefully the comments will shed some light, you need to then set up your php script which will be sent a GET request. (for example, http://some.address.com/phppage.php/?word=casar)
Then you just echo out the output from PHP
<?php
$word = $_GET['word'];
// Connect to some database, get the definitions, and store the results
$result = someDatabaseFunctionThatDoesSomething($word);
echo $result;
?>
Hope this helps, I expect you have a lot of reading to do!

How to display title of page in body content under h1 tag

i have used RSSEO plugin for optimizing my joomla site, however i want my h1 tag in custom components and pages to be similar to page title. Tried below
<h1>
<script type="text/javascript">
<!--
document.write(document.title);
//-->
</script></h1>
The above script is able to display h1 tag, but when checks source code its not seo friendly as display script
I think i need server side php code, have tried using
<h1><?php echo $PageTitle ?></h1>
But above is not displaying any value. only leading to blank h1 tags
Can anyone suggest and advise pls to do it effectively
thanks
Try this:
HTML:
<h1 id="pagetitle"></h1>
JavaScript:
document.getElementById('pagetitle').innerHTML = document.title;
If you want the script inline:
<h1 id="pagetitle"></h1>
<script>
document.getElementById('pagetitle').innerHTML = document.title;
</script>
Joomla $document will contain the title, so simply inject it in your component / template:
<?php
$document = JFactory::getDocument();
echo "<h1>".$document->getMetaData('title')."</h1>";
?>
this should do the trick.
Try this :
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$page = fread(fopen($url, "r"), 2048); // first 2KB
if(preg_match("/<title>(.+)<\/title>/i",$page,$result))
{
echo "The title of $url is $result[1]</b>";
}
else
{
echo "The page doesn't have a title tag";
}
?>
This will load faster as you want :
This works in Joomla! 2.5.x:
<?php
$document = JFactory::getDocument();
?>
<h1><?php echo $document->getTitle(); ?></h1>

echo <body> doesn't activate jquery script

So I have a url from where I extract the data with extracthtml.php:
<?php
include("simple_html_dom.php");
$html = file_get_html($url);
$body = $html->find('body', 0);
$title = $html->find('title', 0);
echo $title;
echo $body;
?>
<script src="extract.js" type="text/javascript"></script>
Then with jquery I put a box around all the p elements to see if this communication works (test, am going to add more css manipulation later). My jquery starts with:
$(document).ready(function(){
$('p').css("border", "2px solid black");
});
Im guessing document.ready is the problem, because there appears to be no boxes around the p elements.
The issue is that you are echoing the jquery line to the browser after you close the page.
Since you echo the $body first, I would guess your page ends up looking something like this:
<body>
...
</body>
<script>
jquery here
</script>
Without seeing your page output this is only a guess, but if it is correct the browser will not run any code after the </body> tag. I would advise checking the source of your output to see if this is the case.

Categories