I have a simple ajax call inside a javascript function to a php file, which searches the DB and returns formatted html. Alls fine, but for some reason the returned html is being wrongly formatted.
Javascript:
$.ajax(
{
url: "getItems.php?lastID=10",
success: function(html)
{
if(html)
{
$("#main").prepend(html);
}
}
});
getItems.php
<?php
mysql_connect();
$lastID = $_GET['lastID'];
$result = mysql_query("SELECT ...");
while ($row = mysql_fetch_assoc($result))
{
echo '<span class="iteminfo"> ';
echo '<a class="username" href="http://x.com/'.$row['UserName'].'" target="_blank"/>'.$row['UserName'].'</a><br/>';
echo '<a class="status" href="http://x.com/'.$row['UserName'].'/c/'" target="_blank" />'.$created_at.'</a></span>';
}
?>
which should return (and it returns this correctly in Firebug)
<span class="iteminfo">
<a class="username" href="http://x.com/username" target="_blank"/>username</a><br/>
<a class="status" href="http://x.com/username/c/" target="_blank" />the date</a>
</span>
but instead its outputting:
<span class="iteminfo">
<a class="username" href="http://x.com/username" target="_blank"/></a>username<br/>
<a class="status" href="http://x.com/username/c/" target="_blank" /></a>the date
</span>
and I've no idea why.
while ($row = mysql_fetch_assoc($result))
{
echo '<span class="iteminfo"> ';
echo '<a class="username" href="http://x.com/'.$row['UserName'].'" target="_blank">'.$row['UserName'].'</a><br/>';
echo '<a class="status" href="http://x.com/'.$row['UserName'].'/c/'" target="_blank" >'.$created_at.'</a></span>';
}
You have closed a tag wrong manner
<a> content </a>
but u have used
<a/></a>
It's a fairly simple problem -- you're closing your anchor tags and then trying to close them again. The HTML spec I think gives browsers the option of creating an additional tag here.
Here's the code you want to change:
echo '<a class="username" href="http://x.com/'.$row['UserName'].'" target="_blank">'.$row['UserName'].'</a><br/>';
echo '<a class="status" href="http://x.com/'.$row['UserName'].'/c/'" target="_blank" >'.$created_at.'</a></span>';
}
The only difference is that the <a> tags are terminated by '>' and not '/>' which would imply that you're closing it at the same time.
Related
I have this code, i get the info but with this i get the data + the link for example
require_once('simple_html_dom.php');
set_time_limit (0);
$html ='www.domain.com';
$html = file_get_html($url);
// i read the first div
foreach($html->find('#content') as $element){
// i read the second
foreach ($element->find('p') as $phone){
echo $phone;
Mobile Pixel 2 -
google << there the link
But i need remove these link, the problem is the next, i scrape this:
<p>the info that i really need is here<p>
<p class="text-right"><a class="btn btn-default espbott aplus" role="button"
href="brand/google.html">Google</a></p>
I read this:
Simple HTML Dom: How to remove elements?
But i cant find the answer
update: if i use this:
foreach ($element->find('p[class="text-right"]');
It will select the links but can't remove scrapped data
You can use file_get_content with str_get_html and replace it :
include 'simple_html_dom.php';
$content=file_get_contents($url);
$html = str_get_html($content);
// i read the first div
foreach($html->find('#content') as $element){
// i read the second
foreach ($element->find('p[class="text-right"]') as $phone){
$content=str_replace($phone,'',$content);
}
}
print $content;
die;
Or here a native version:
PHP-CODE
$sHtml = '<p>the info that i really need is here<p>
<p class="text-right"><a class="btn btn-default espbott aplus" role="button"
href="brand/google.html">Google</a></p>';
$sHtml = '<div id="wrapper">' . $sHtml . '</div>';
echo "org:\n";
echo $sHtml;
echo "\n\n";
$doc = new DOMDocument();
$doc->loadHtml($sHtml);
foreach( $doc->getElementsByTagName( 'a' ) as $element ) {
$element->parentNode->removeChild( $element );
}
echo "res:\n";
echo $doc->saveHTML($doc->getElementById('wrapper'));
Output
org:
<div id="wrapper"><p>the info that i really need is here<p>
<p class="text-right"><a class="btn btn-default espbott aplus" role="button"
href="brand/google.html">Google</a></p></div>
res:
<div id="wrapper">
<p>the info that i really need is here</p>
<p>
</p>
<p class="text-right"></p>
</div>
https://3v4l.org/RhuEU
How is it possible to use the href for this full div tag?
<?php
echo '<div id="" onclick="location.href="viewpost.php";" style="cursor:pointer;"> test </div>';
echo "<div class='nieuwscol' onclick='location.href='viewpost.php';' style='cursor:pointer;'>";
echo "<img class='imgnews' src='".$row['imgNewspost']."'>";
//echo '<p>'.$row['postAuteur'].'</p>';
echo '<div class="newstitle"><p>'.$row['postTitle'].'</p></div>';
echo '<p>'.$row['postDesc'].'</p>';
echo '<a style="font-size: 14px;" href="viewpost.php?id='.$row['postID'].'#commentsID"><img src="img/commenticon.png" alt="" height="20" width="20" ><div class="newsdate" float: right;> <p style="font-size: 14px;"> 000 </a>'.date('jS M Y', strtotime($row['postDate'])).'</p></div>';
echo '</div>';
echo '</a>';
?>
As you can see, the a href is working for newstitles, but now I would like to use the same weblink for the whole div.
Any suggestions?
You should set the url with the div like :
<div data-bind="<?php echo 'viewpost.php?id='.$row['postID'];?>" class="div-class"></div>
Then write jquery function like following:
$(document).on("click",".div-class",function(){
window.location = $(this).data('bind');
});
Simply add <a href=""> which contains whole div.
In your code we see </a> in the last echo which don't have start anywhere above first echo;
I want to use html syntax in php and i want to use . instead of <?php ?> my code ig given below ,
$delete='<a class="confirm" onclick="return delete_event(<?php echo $value->id; ?>, '<?php echo base_url() . 'webtv/delete_channels' ?>', '<?php echo current_full_url(); ?>');" href="" ><i class="glyphicon glyphicon-trash text-red del" title="Delete"></i></a>';
Please help me.
<?php
$value = "value_1"; //$value->id;
$baseurl = "base_url"."/webtv/delete_channels"; //base_url()."/webtv/delete_channels";
$fullurl = "current_url"; //current_full_url();
$delete="<a href='#' class='confirm' onclick=\"return delete_event('".$value."','".$baseurl."','".$fullurl."')\" >Click me</a>";
echo $delete;
?>
<script>
function delete_event(var1,var2,var3){
alert(var1+" -- "+var2+" -- "+var3);
}
</script>
You can use sprintf function to solve this issue.
Solution is:
$delete = sprintf('<a class="confirm" onclick="return delete_event(%d, \'%s\',\'%s\');" href="" ><i class="glyphicon glyphicon-trash text-red del" title="Delete"></i>jkhklh</a>', $value->id, base_url() . 'webtv/delete_channels', current_full_url());
I cleaned up your php syntax. I don't quite understand what you're trying to do, but the declaration below should give you an a tag containing what you want (a confirm delete button):
$delete='<a class="confirm" onclick="return delete_event($value->id, '.base_url().'webtv/delete_channels, '.current_full_url().');" href="" ><i class="glyphicon glyphicon-trash text-red del" title="Delete"></i></a>';
If you echo this on your webpage, assuming all of your other links are correct, it should work.
I´m trying to style the following php echo
<a href="" title="<?php
echo"
<div class='test'>".$name."</div>'/n'
<div class='test2'>".$itemLevel."</div>'/n'
";
?>">Test Link</a>
But i think i don´t insert the div class and the '/n' at the right place. Could anyone help me?
You don't echo the HTML content inside the a href's title.
This will work:
<a href=""><?php echo"
<div class='test'>".$name."</div>'/n'
<div class='test2'>".$itemLevel."</div>'/n'
";
?>Test Link</a>
Optimizing your code:
<?php echo '
<a href="">
<div class="test">'.$name.'</div>
<div class="test2">'.$itemLevel.'</div>
Test Link</a>';
You must encode your html in order to put it into the title property
<?php
$title = '<div class="test">'.$name.'</div>' . PHP_EOL .
'<div class="test2">'.$itemLevel.'</div>' . PHP_EOL;
?>
Test Link
In my php file I am echoing back an html response to my jQuery function.
Due some wrong quoting that I am applying data is no being passed through onclick event
echo " <div class='img-x' id='img-x-".$newdata['id']."' ><span onclick='test('".$newdata['id']."','".$newdata['image_path']."');' class='icon-remove'></span></div> <img src='http://localhost/corridor/uploads/".$actual_image_name."' class='img_prvw' id='img-x-".$newdata['id']."' />";
in html it shows like this
<span onclick="test(" 164','13906347455190.jpg');' class="icon-remove"></span>
Where am I doing it wrong?
Try escaping the quotes properly.
echo "<div class='img-x' id='img-x-".$newdata['id']."'><span onclick=\"test('".$newdata['id']."','".$newdata['image_path']."');\" class='icon-remove'></span></div> <img src='http://localhost/corridor/uploads/".$actual_image_name."' class='img_prvw' id='img-x-".$newdata['id']."' />";
^
I am assuming you are ajaxing this in:
echo <<< _html
<div class="img-x" id="img-x-$newdata['id']">
<span data-id="$newdata['id']" data-imagepath="$newdata['image_path']"
class="icon-remove"></span>
</div>
<img src="http://localhost/corridor/uploads/$actual_image_name"
class="img_prvw" id="img-x-$newdata['id']" />
_html;
and in the succes:
success: function() {
$("#someContainer").html(data);
$("#someContainer .icon-remove").on("click",function() {
test($(this).data("id"),$(this).data("imagepath"));
});
}
If no ajax then
echo <<< _html
<div class="img-x" id="img-x-$newdata['id']">
<span onclick="test('$newdata['id']','$newdata['image_path'].')"
class="icon-remove"></span>
</div>
<img src="http://localhost/corridor/uploads/$actual_image_name"
class="img_prvw" id="img-x-$newdata['id']" />
_html;