1

I have a div named "main" in my page. I put the code to convert a html into pdf using php at the end of page. I want to select the content (div named main contains paragraphs, charts, tables etc.).

How ?

j0k
  • 22,600
  • 28
  • 79
  • 90
Nishil Athikkal
  • 69
  • 1
  • 10
  • 1
    http://stackoverflow.com/questions/8647216/get-content-of-a-div-using-javascript – Peon Jan 18 '13 at 09:52
  • hi dainis , not using jquery using php? pls help me – Nishil Athikkal Jan 18 '13 at 09:54
  • 1
    1st javascript, not jquery. 2nd, php is server side language, it has no access to client side inputs. If you want to pass the value to php, use javascript to fetch it and ajax to send it to php. – Peon Jan 18 '13 at 09:55

3 Answers3

2

Below code will show you how to get DIV tag's content using PHP code.

PHP Code:

  <?php
    $content="test.html";
    $source=new DOMdocument();
    $source->loadHTMLFile($content);
    $path=new DOMXpath($source);
    $dom=$path->query("*/div[@id='test']");
    if (!$dom==0) {
       foreach ($dom as $dom) {
          print "
    The Type of the element is: ". $dom->nodeName. "
    <b><pre><code>";
          $getContent = $dom->childNodes;
          foreach ($getContent as $attr) {
             print $attr->nodeValue. "</code></pre></b>";
          }
       }
    }
  ?>

We are getting DIV tag with ID "test", You can replace it with your desired one.

test.html

<div id="test">This is my content</div>

Output:

The Type of the element is: div
This is my content
Moin Ahmed
  • 2,898
  • 21
  • 33
1

You should put the php code into a separate file from the html and use something like DOMDocument to get the content from the div.

$dom = new DOMDocument();
$dom->loadHTMLFile('yourfile.html');
...
0

You cannot directly interact with the HTML DOM via PHP. What you could do, is using a with an input containing your content. When submitting the form you can access the data via PHP.

But maybe you want to use Javascript for that task?

Nevertheless, a quick'n'dirty PHP example:

<form action="" method="post">
    <textarea name="content">hello world</textarea>
</form>

<?php
   if (isset($_POST['content'])) {
       echo $_POST['content'];
   }
?>
aeno
  • 560
  • 5
  • 24
  • if he needs to access the DOM, I'm ok with Dainis Abols, he'd rather to send the DOM through AJAX and get it with PHP... – Louis XIV Jan 18 '13 at 10:00
  • my div named main contain large amount of data , then is it possible to send through ajax ? – Nishil Athikkal Jan 18 '13 at 10:00
  • Nishil, it could be possible (depending on your webserver config). But i really think you should be using client-side scripting, as Louis and Dainis suggested. What exactly do you want to do? You could give a more detailed example. Maybe you need a whole different approach. – aeno Jan 18 '13 at 10:02
  • aeno , i have a php page it fetch data from flurry and other and creating highcharts tables etc. then my problem is , if any user want it in pdf format i want to convert when a download pdf link clicks, pls help me – Nishil Athikkal Jan 18 '13 at 10:06
  • where does your chart data come from? could you directly use that data source with PHP? – aeno Jan 18 '13 at 10:07
  • data comes from 5 different tables and i created the chart, then i want to convert it into pdf so that all the details contains in a div named "main", i want to take the contant of that div into my php code how? – Nishil Athikkal Jan 18 '13 at 10:10
  • Are you talking about HTMl `` elements or database tables? When you mean HTML `
    ` elements, consider transferring them into a database from which PHP can read the data and process it into a PDF.
    – aeno Jan 18 '13 at 10:24
  • got it aeno thanks. but i have a doubt, me using tcpdf library when i get pdf it is all in same font format , how we can change it – Nishil Athikkal Jan 18 '13 at 10:39
  • great! i think this belongs in a new question, though ;) – aeno Jan 18 '13 at 10:40