Get all <a class="execute" href=" - links and display below - javascript

I want to take all links that begin with
<a class="execute" href="
from
https://bitbucket.org/alceawisteria/ostr/issues
and then display them below in the current HTML document.
Is this possible with js?
(If not, how can it be done otherwise ?)
Tried to implement approaches from the "GitHub issue widget" code to no avail.

This solves the issue via PHP link scraping
<?php
require 'simple_html_dom.php';
$dom = new DOMDocument;
#$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');
$url = 'https://bitbucket.org/alceawisteria/ostr/issues/';
$html = file_get_contents($url);
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//a[#class="execute"]');
foreach ($nodes as $node){
echo $link->nodeValue;
echo "<a target='_blank' href=";
echo "https://bitbucket.org";
echo $node-> getAttribute('href');
echo ">";
echo $node-> getAttribute('href');
echo "</a>", '<br>';
}
?>

Related

How to echo node value with javascript (on load)?

So I have this code that loads php, when the page loads:
<script>
$(document).ready(function(){
$('#time').load('https://www.mywebsite.com/trigger.php');
});</script>
<div id="time"></div>
and I have trigger.php:
<?php
$parent_title = get_the_title( $post->post_parent );
$html_string = file_get_contents('https://mywebsite.com');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$values = array();
$row = $xpath->query('//*[#id="canvass-0-CanvassApplet"]/div/ul/li[2]/div/div[2]/div[1]');
foreach($row as $value) {
print($value->nodeValue);
}
?>
but it doesn't load.
I know I need to echo in the .php first.
So I have tried to echo $value; instead of print, but still nothing.
When I type something simple like <?php echo 'heyyy load me';> then it does work.
How do I echo my php code?
Need help, thanks.

Add onclick event to a dynamic created link (PHP)

I have many links which are dynamically created with PHP and now I want to add an onclick event.
while($row = mysqli_fetch_array($xyz)) {
echo "<tr>";
echo "<td><a href=\"test.php?parameter=". $row['z'] ."\" target=\_blank>" . $row['z'] . "</a></td>";
echo "</tr>";}
but I want like this:
while($row = mysqli_fetch_array($xyz) {
echo "<tr>";
echo "<td><a href=\"test.php?parameter=". $row['z'] ."\" target=\_blank>" . $row['z'] . onclick="test()"</a></td>";
echo "</tr>";}
is this possible?
I am not sure if I understood what exactly you are asking but I try to answer you the same:
You can't record a normal JS variable in a page session and then to access to it after the loading of a page.
For this case you can use cookies (that are accessible via server-side too):
// put this code into methods that users suggested you.
document.cookie = "value_selected=something"
and once page is loaded:
var cookies = document.cookie.split(';');
var value_selected = JSON.parse(JSON.stringify(cookies[YOUR_COOKIE].split("=")[0]))
or server side:
echo "<td><a href=\"test.php?parameter=". $row['z'] ."\" data-something=htmlentities($_COOKIE['value_selected']) target=\_blank>" . $row['z'] . "</a></td>"; //(example)
Or more simply, pass the value selected through query
?value_selected=something //elaborate the new link in the method users suggested you
and
in server-side, use $['GET'] to obtain all values in query string.
IMHO I think the first choice is simpler than second.
I hope I helped you
Try Like this
while($row = mysqli_fetch_array($xyz) {
echo "<tr>";
echo "<td><a class='myclick' href=\"test.php?parameter=". $row['z'] ."\" target=\_blank>" . $row['z'] . **onclick="test()"**</a></td>";
echo "</tr>";}
i have added a class to link as myclick
and in jquery
$(document).on('click','.myclick',function(e){
e.preventDefault();
alert('clicked');
});
You can try like this..just a simple way.
<script type="text/javascript">
function test(){
alert('test');
}
</script>
<?php
while($row = mysqli_fetch_array($xyz) {
echo "<tr>";
echo "<td><a href='your link' onclick='test()'></a></td>";
echo "</tr>";
}
?>

pagination doesn't work when I click on button javascript and php

My pagination returns data from the database but when I click for example the second page button it say:
The requested URL /MFPTMA/poserquestion-Copie.php was not found on this server.
<?php
$sql = "SELECT COUNT(idra) FROM reponsea";
$rs_result = mysqli_query($conn, $sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / $limit);
$pagLink = "<nav><ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li>
<a href='poserquestion-Copie.php?page=".$i."'>".$i."
</a>
</li>";
}
echo $pagLink . "</ul></nav>";
?>
<script type="text/javascript">
$(document).ready(function(){
$('.pagination').pagination({
items: <?php echo $total_records;?>,
itemsOnPage: <?php echo $limit;?>,
cssStyle: 'light-theme',
currentPage : <?php echo $page;?>,
hrefTextPrefix : 'poserquestion-Copie.php?page='
});
});
</script>
If you put the URL like this:
<a href='poserquestion-Copie.php?page=".$i."'>
This will be a relative URL. This URL is relative to the current path. What this means is that it will resolve to different paths depending on where you are in the site.
The best practice here is you should put a absolute URL here. Absolute URL can be:
<a href='/poserquestion-Copie.php?page=".$i."'>
Or:
<a href='/<custom path>/poserquestion-Copie.php?page=".$i."'>
Or even including domain of your site:
<a href='<domain>/<custom path>/poserquestion-Copie.php?page=".$i."'>

How to retrieve $_SESSION value in a JavaScript/HTML

I have this working script yet when I change it to retrieve(supposedly) the value inside $_SESSION["username"], it doesn't retrieve anything. The whole page is saved as .php as I am using some codes that uses PHP.
Code:
echo "<script type=text/javascript>";
echo "var hidden = false;";
echo "function actiondb1() {";
echo "if(!hidden) {";
echo "document.getElementById(\'clickdb1\').style.visibility = \'hidden\';";
echo "document.getElementById(\'db1\').style.visibility = \'visible\';";
echo "document.getElementById(\'db1\').disabled = false;";
echo "document.getElementById(\'db1\').value =".$_SESSION["username"];.";";
echo "}";
echo "}";
echo "</script>";
How can I make the script to properly retrieve the data inside $_SESSION["username"];?
Observe that, for instance, if the value of $_SESSION["username"] is John, your echo will be generating this result:
document.getElementById('db1').value = John;
But John is supposed to be a string and should be wrapped in quotation marks, otherwise JavaScript will understand it as a variable name (which value will be probably undefined).
As Havenard mentioned, this line is missing Javascript single quotes to properly denote a string variable:
echo "document.getElementById(\'db1\').value ='".$_SESSION["username"];."';";
However, you really shouldn't print JS out with PHP if you can help it. Though iatboy's answer answer won't ultimately fix your bug, it is a much cleaner way of doing things.
?>
<script type=text/javascript>;
var hidden = false;
function actiondb1() {
if(!hidden) {
document.getElementById('clickdb1').style.visibility = 'hidden';
document.getElementById('db1').style.visibility = 'visible';
document.getElementById('db1').disabled = false;
document.getElementById('db1').value ='<?php echo $_SESSION["username"];?>';
}
}
</script>;
<?php
Did you start session in this page?If you didn't,use the follow code to start session.
session_start();
Then change your code to
echo "<script type=text/javascript>";
echo "var hidden = false;\n";
echo "function actiondb1() {\n";
echo "alert('".$_SESSION['username']."')\n"; //test code
echo "if(!hidden) {\n";
echo "document.getElementById('clickdb1').style.visibility = 'hidden';\n";
echo "document.getElementById('db1').style.visibility = 'visible';\n";
echo "document.getElementById('db1').disabled = false;\n";
echo "document.getElementById('db1').value ='".$_SESSION["username"]."';\n";
echo "}\n";
echo "}\n";
echo "</script>";
it will be ok.

How can I pass PHP GET URL variables to open a window with Javascript?

I'd like to pass a PHP get variable from a link to Javascript so I can open a new smaller window with the appropriate content from the value passed to the URL .. I tried to do that below and I couldn't ... I'd really appreciate your assistance. The code below generates image hyperlinks that each have their ids from the database, so when the image is clicked on, a new window should be open but the ID should be passed to the javascript window.open method ... I tried to do that with AJAX to load the content according to the get variable but I couldn't do it!
<?php
require('../database/connect.php');
database_connect();
$query = "select * from Entertainers";
$result = $connection->query($query);
$row_count =$result->num_rows;
for($i = 1; $i <= $row_count; $i++)
{
$row = $result->fetch_assoc();
?>
<?php echo "<a href='' onclick='window.open(profile.php?id=".$row['ID'].")'><img src ='../".$row['Picture']."' width='100' height='100' /> </a>"; } ?>
Don't forget to quote the url in the Javascript open function. Also, did you consider using printf() for outputting?
$link =
'<a href="" onclick="window.open(\'profile.php?id=%d\')">'
. '<img src="../%s" width="100" height="100" /></a>' . PHP_EOL;
for($i = 1; $i <= $row_count; $i++) {
$row = $result->fetch_assoc();
printf($link,$row['ID'],$row['Picture']);
}
%d represents a decimal and %s represents a string in the above string (hence the $link). Another tip: if you have no particular reason to use the for loop, using a while loop instead will make your code cleaner and shorter.
while ($row = $result->fetch_assoc()) {
printf($link,$row['ID'],$row['Picture']);
}
When your script renders the HTML on the page the URL parameter to window.open is not being rendered as a string.
What your code currently renders on the page:
<a href='' onclick='window.open(profile.php?id={some_id})'><img src ='../".$row['Picture']."' width='100' height='100' /> </a>"; } ?>
The URL profile.php?id={some_id} is not a string when it is being parsed at the client.
Try this:
<?php echo "<img src ='../".$row['Picture']."' width='100' height='100' /> "; ?>

Categories

Resources