April 3, 2013

how can i obtain an entire element from an external page?

Cola89’s Questions:

how can i get a

<table class="precios"> 

from another url (www.example.com) in HTML format?? because with DOM i can obtain the table but in an array mode.

Thank you everyone for helping

Assuming you have no cross-domain issues, you can use .load() for that:

$container.load('http://www.example.com/path/to/page table.precios');

Whereby $container is a jQuery object where you want to “save” the table into.

Within PHP you would solve it this way:

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTMLFile('http://www.example.com/path/to/page.html');
libxml_clear_errors();
$xp = new DOMXPath($doc);
$table = $xp->query('//table[@class="precios"]')->item(0);

echo $doc->saveHTML($table);

Make a AJAX request to that page and use .find() to get the element

$.ajax( {
  url: myUrl,
  success: function(html) {
    table = $(html).find(".precious");
    }
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!