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 ?

3
  • 1
    stackoverflow.com/questions/8647216/…
    – Peon
    Commented Jan 18, 2013 at 9:52
  • hi dainis , not using jquery using php? pls help me Commented Jan 18, 2013 at 9: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
    Commented Jan 18, 2013 at 9:55

3 Answers 3

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
0
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'];
   }
?>
9
  • 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
    Commented Jan 18, 2013 at 10:00
  • my div named main contain large amount of data , then is it possible to send through ajax ? Commented Jan 18, 2013 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
    Commented Jan 18, 2013 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 Commented Jan 18, 2013 at 10:06
  • where does your chart data come from? could you directly use that data source with PHP?
    – aeno
    Commented Jan 18, 2013 at 10:07

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