Using PHP DOM parser to get data from Javascript code - javascript

I'm learning about parsers and after taking a look at Java parser JSoup I'm trying to do the same with PHP. I'm using PHP Simple HTML DOM (don't now if it's the best solution) to get some data from an url, but without success. How can I take the 8 (near bar1) and 18 (near bar0) numbers from the next code from external web using a PHP parser?
<table>
<tr><td><div name='divns6' id='divns6' style='position:relative;visibility:hidden;'
width='400' height='160'><table valign=botton cellpadding='0' cellspacing='0'
border='0'><tr valign='bottom'>
<td width=15 valign="bottom" height=150><a href="javascript:void(null)"
onMouseOver="changeImage('bar1','','47',2);activadiv('bar0','18');"
onMouseOut="changeImage('bar1','','47',0);desactivadiv('bar1');"><img
NAME="barra1" width="11px" height="47" border="0"></a></td>
<td width=15 valign="bottom" height=150><a href="javascript:void(null)"
onMouseOver="changeImage('bar2','','21',2);activadiv('bar1','8');"
onMouseOut="changeImage('bar2','','21',0);desactivadiv('bar2');"><img
NAME="barra2" width="11px" height="21" border="0"></a></td>
</td></tr></table>
Here's what I've tried, but It only shows the entire line, not only the number:
<?php include ("simple_html_dom.php");
// Create DOM from URL or file
$html = file_get_html('http://www.myurl.com');
// Find all text blocks
$ret = $html->find('a[onMouseOver]');
for ($count='0'; $count<count($ret);$count++) {
echo $ret[$count];
}
?>
With JSoup the line who do the trick was: String onMouseOver = doc.select("a").attr("onMouseOver");
Thank you in advance for your time and dedication.

Finally I've found the solution on my own!!
<?php include ("simple_html_dom.php");
// Create DOM from URL or file
$html = file_get_html('http://www.myweb.com');
// Find all text blocks
$ret = $html->find('a[onMouseOver]');
for ($count='0'; $count<count($ret);$count++) {
echo split("'", $ret[$count])[9]." ";
}
?>

Related

how to get the td id in JavaScript

<table class="table" style="margin-bottom:0px!important">
<b>
<? if(!empty($channelBase['rep_ids']))
{
$s_id=$channelBase['id'];
$i=0;
$temp='';
$reps_channl=explode(",",$channelBase['rep_ids']);
foreach($reps_channl as $k)
{
$added_reps = $rep_names[$k];
if($i==0){
$temp.='<tr style="border-top:none;">';
}
$temp.="<td id='".$s_id."_".$k."' class='repclicked' style='border-top:none;'>$added_reps <a href='#' class='btn btn-xs btn-icon btn-circle ' onclick='delete_repid($k,$s_id);'><i class='fa fa-close'></i></a>
</td>";
$i++;
if($i==5)
{
$temp.='</tr>';
$i=0;
}
}
echo $temp;
}
?>
</b>
</table>
Here I have a td id with a number value. here I am getting confusion on how to get the td id in javascript. can anyone please help me.
Here is how you can get Ids of td inside table
for (let row of mytab1.rows)
{
for(let cell of row.cells)
{
console.log(cell.id)
}
}
<div id="myTabDiv">
<table name="mytab" id="mytab1">
<tr>
<td id="id_1">col1 Val1</td>
<td id="id_2">col2 Val2</td>
</tr>
<tr>
<td id="id_3">col1 Val3</td>
<td id="id_4">col2 Val4</td>
</tr>
</table>
</div>
Try this,
"<td id='".$s_id."_".$k."' onclick='alert(this.id);'></td>"
You are trying to mix PHP and javascript, which you can not do. Everything in PHP is separate from javascript and the two do not have access to identifiers from each other.
The reason is this:
PHP code is completely run BEFORE the page is loaded. Once the page is loaded completely in PHP, it is then sent to the browser where the javascript acts upon whatever the result of the PHP code was.
Try this:
on a page enter:
<pre>
<?php
$a = "1";
$b = "2";
print_r($a);
print_r($b);
?>
</pre>
Now load the page and right click on the page and go to "inspect " or "view source"
you will see that the source of the page has no php code and is only
<pre>12</pre>
This is because all PHP code is processed BEFORE the page is loaded, whereas javascript is processed AFTER the page is sent to the browser.
You have to do everything involving PHP Ids first and completely separate from javascript, and likewise javascript must be completely separate from PHP.
For your case you must make a separate request in order to modify an array in PHP and then either reload the page or use AJAX to load the updated data.
The type of action you are trying to accomplish is impossible the way you are trying to do it.

How to download big size images using <a> tag

I'm trying to connect to the database and view pictures from there. Despite the fact that all pictures are shown, it is not possible to download them. To download I use tag .
Here is my code.
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td align="center">
<a href="data:image/jpeg;base64,'.base64_encode($row['Pilt'] ).'" download>
<img src="data:image/jpeg;base64,'.base64_encode($row['Pilt'] ).'" width=400 class="img-thumnail" />
</a>
</td>
</tr>
';
}
Here is a small part of the database where I get pictures. https://imgur.com/yZGsFzb . So I noticed that only one photo is being downloaded, which size is 150.4KiB.
I expect that by clicking on the image (any size) it will be downloaded. Right now when I click on image appears error like this https://imgur.com/yZrlIH8 whereas I would like to see something like this https://imgur.com/4vkUukp .
I will be very grateful for any help.
I recommend you stop printing large quantities of big images that way, they will be hardcoded in the HTML code and downloaded all at once to the browser's cache, 2 times (3 with the click included). This wastes RAM and bandwidth. The network error you get is probably caused by the abnormally big size of the href in your link (might even look a bit like malware to your browser).
Instead, you want to serve these images dynamically on a PHP script first (by setting the header to 'Content-Type: image/png' and printing the image contents as obtained from your database) and provide a direct URL to the script.
This example works just as described in your screenshots, I'm indicating where you should replace your SQL queries to obtain the image:
while(for all your rows)
{
echo '
<tr>
<td align="center">
<a href="image.php?id=' . $id . '" download>
<img src="image.php?link=' . $id . '" width=400 class="img-thumnail" />
</a>
</td>
</tr>';
}
And image.php would look something similar to:
<?php
$id = htmlspecialchars($_GET['id']);
header('Content-Type: image/png');
$content = store the image contents from the BD here ($row['Pilt'] in your code);
echo $content;
Notice that image.php serves images dynamically and I'm supposedly identifying each image with an id (perhaps your row id? You can think of more secure identifiers too if ids are not public already). This is a common security practice in PHP so that you're not providing absolute file paths or other information to attackers.

How to export dynamic html table to doc file using php?

I'm new using PHP and I need a bit of help here.
I'd like to send a HTML table to another PHP file, and then, be to able to use this information (specifically I want to download this like DOC file).
I've seen a lot of information how to do it. But I haven't seen how to do without <tbody></tbody>. I have a dynamic table, so, the data is loading from an array. By the way, I'm using DataTable-jQuery to do it.
I have the following HTML code:
<form action="sectoresTable.php" method="post">
<table id="sectoresTable">
<thead>
<tr>
<th><b>#</b></th>
<th><b>Numero</b></th>
<th><b>Nombre</b></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<button type="submit" style="margin-top: 20px;">Exportar</button>
</form>
and sectoresTable.PHP:
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=TablaSectores.doc");
echo '';
?>
By the way, to load the data into the table, I'm using the following script:
<script>
$('#sectoresTable').DataTable({
data: arraySectores
});
</script>
In general all this is working good, I download a doc file but without information (and That is right because my echo is printing nothing.).
I understand that I need to use a foreach in my HTML code? But really, I'm not sure.
try to use an api,to export dynamic html table to doc file in php
http://www.phpclasses.org/package/2763-PHP-Convert-HTML-into-Microsoft-Word-documents.html

inserting popup window command in sql

I'm assigning popup window on my links but it doesn't work sorry i'm still learning about mysqli and javascript.
while($row = mysqli_fetch_array($result))
{
$id = $row['BapID'];
echo "<tr>";
echo "<th>" . "<a href=bapview.php?BapID=$id onlick='pop_up(this);'>View Full Info</a>" .
" | " .
"<a href=bapupdate.php?BapID=$id onlick='pop_up(this);'>Edit</a>" . "</th>";
My script
function pop_up(url){
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=1076,height=768,directories=no,location=no') }
The url that you want the pop-up window to point should be put as the parameter to your function call in your onclick handlers:
<a onclick="pop_up('bapupdate.php?<?php echo $id; ?>');">Edit</a>
As you can see at
https://developer.mozilla.org/en-US/docs/Web/API/Window.open
the first parameter to the window.open method is a string value for the url you want the opening window to point to. Your code was attempting to provide as that argument the actual reference to the link element on your page (that's what this will refer to in that context).
A couple of suggestions:
Don't be afraid to 'exit' (?>) PHP half way through a script. This will make it easier to read later on and allows you to write in pure HTML.
It looks like your missing you quotation marks around your href parameter. This likely won't cause issues but you never know. I would also recommend using the full url in your href and also as your pop_up() parameter (as suggested by #myesain)
<?php
while($row = mysqli_fetch_array($result)){
$id = $row['BapID']; ?>
<tr>
<th>
<a href=# onclick='pop_up("http://fullurl.com/bapview.php?BapID=<?php echo $id; ?>");'>View Full Info</a>
|
<a href=# onclick='pop_up"http://fullurl.com/bapupdate.php?BapID=<?php echo $id; ?>");'>Edit</a>
</th>
</tr>
<?php
//Rest of code
?>

Clean way to Template an HTML code block that only takes title/content parameters, where content may be large and littered with new-lines

I am using Accordion (jQuery) on my school webserver. Currently, my coding-scheme uses PHP/HTML/CSS/Javascript. I started noticing an opportunity for automation/templating when writing the entries for the Accordion modules. I write the following code:
<h3>Title</h3>
<div class="nobg">
<p class="nobg">
<!-- Entry text -->
</p>
</div>
so I am looking for pointers for the best way to template that code based on the following needs:
Adjustable parameters: Title, Content
When making new modules with a large content 'parameter', the creation of that parameter should maintain readability.
Since I am already on PHP, I was thinking maybe some sort of template function:
<? php accordion_entry("Title", "Entry Text" ?>
But the text is usually a lot of HTML: like the following:
PDF
<p>
The release date is 2007 but the pinout seems to check out (I did some small verifications with my PCB). Also, the reference documents are all valid!
</p>
I would like to write that HTML myself in the designated spot where the module will eventually manifest as a whole. Perhaps even cooler would be something like this:
<accordion-entry title="Title">
PDF
<p>
The release date is 2007 but the pinout seems to check out (I did some small verifications with my PCB). Also, the reference documents are all valid!
</p>
</accordion-entry>
I have no idea how to get started creating such a mechanism, or if it's too much trouble to bother.
I found my temporary solution, until someone comes along with something better! Please review! I am no PHP Expert!! :D
The PHP Function:
<?php
function accordionEntry($title, $entry)
{
echo '<h3>' . $title . '</h3>';
echo '<div class="nobg">';
echo ' <p class="nobg">';
echo $entry; // <!-- Entry text -->
echo ' </p>';
echo '</div>';
}
?>
The PHP function call:
<?php accordionEntry(
"GSM0107IG001 - Integration Manual",
'PDF
<p>
The release date is 2007 but the pinout seems to check out (I did some small verifications with my PCB). Also, the reference documents are all valid!
</p>');
?>
Create a partial, and load your content into it along with settings
accordian.phtml (just use .html if you want, doesn't really matter)
<accordion-entry title="<?php $title ?>">
<?php $content ?>
</accordion-entry>
page.html
<div><?= renderPartial('accordian.phtml',array(
'title'=>'GSM0107IG001 - Integration Manual',
'content' => '<p>your html</p>'
)); ?>
partial.php
function partial($partial, $settings){
//will load html from indicated file, and merge passed settings and content into place before returning all $html
// this allows the reuse of the 'partial()' function for other snippets
$template = file_get_contents($partial);
//$settings should be an array, and then your keys can be extracted as variables that match the $settings variables (such as $title) that exist in the .html partial file
extract($settings); //will assign any keys in your array, such as 'title' to php variables of the same name... so in this case $title, and $content
echo $template;
}

Categories