I would like to populate a jQuery listview in a PHP loop, and I attempted to do so by echoing javascript code that populates the list with a PHP variable. This is what I'm working with:
My HTML
<div data-role='page' id='feedPage'>
<div data-role='content'>
<ul id='pics' data-role='listview'>
<li>test</li>
</ul>
</div>
</div>
and my PHP / JavaScript
echo "<script type='javascript'>
var pics = \$('#pics')
var pitem = \$('<li/>').html($myArray[element])
var plink = \$('<a/>')
pitem.append(plink)
pics.append(pitem)
pics.listview('refresh')
</script>";
but the list comes up blank. This code is running inside of a PHP for loop, and I am able to access and manipulate all the elements of $myArray just fine in PHP, but I cannot seem to populate the list. I even tried running this code with a simple .html('hello') to no avail. All I get is a blank list with the exception of the test item I hardcoded in the HTML. Is there a way to generate a list in PHP like this, and if so, how can I do it properly?
Thanks!
SOLUTION:
I got this working simply by doing <script type='text/javascript'> and .html('$myArray[element]') (notice the single quotes). This works because the javascript is running inside a PHP echo. Oh, and none of my $ needed to be escaped. Final code:
echo "<script type='text/javascript'>
var pics = $('#pics')
var pitem = $('<li/>').html('$myArray[element]')
var plink = $('<a/>')
pitem.append(plink)
pics.append(pitem)
pics.listview('refresh')
</script>";
I think the main problem is in type='javascript', it should be type='text/javascript'.
Another thing to consider is that the content of the $myArray[element] needs to be printed as a javascript string. Running $myArray = array_map('json_encode', $myArray); should do the trick.
You forgot to put your JS-code into document.ready event callback
$(document).ready(function($){
var pics = $('#pics')
var pitem = $('<li/>').html($myArray[element])
var plink = $('<a/>')
pitem.append(plink)
pics.append(pitem)
pics.listview('refresh')
});
Related
I'm new to javascript.
There seems to be no result but a blank screen when I try the following code:
function send_to_class($data)
{
echo 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP';
?>
<script type="text/javascript">
$(".username").append(<?php echo $data;?>);
</script>
<?php
echo 'EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE';
}
and my PHP
$data = "You might think I'm Bonkers, but I just think I'm Free";
send_to_class($data);
and my html
<span class="username"></span>
The Function is declared before the send_to_class($data) is initiated.
The P's and the E's are showing up just fine, there's just no middle sentence.
I've tried divs, id's and classes, adding css and changing the script tag.
2 things, a) how do I get the function to work which appends $data to the .username class and b) how do I get it to work if $data is an array? Can Javascript print php arrays in this manner?
Thanks.
I found that because the $data I was sending contained "'s that it needed to have the "'s escaped. So in my php file that was producing the data, I had to turn the following:
<div class="class1">some words</div>
into this:
<div class=\"class1\">some words</div>
This is because it's javascript that's doing the printing, and when javascript does printing of "'s it needs to have them escaped with a backslash like this \".
I am trying to get the following text "Huggies Pure Baby Wipes 4 x 64 per pack" shown in the code below.
<div class="offerList-item-description-title">
<div id="result-title-5" class="offerList-item-description-title">
<script type="text/javascript">
document.write(getContents('wF8UD9Jj8:6D !FC6 q23J (:A6D c I ec A6C A24\<'));
</script>Huggies Pure Baby Wipes 4 x 64 per pack
</div>
</div>
I have tried using code such as:
foreach($element -> find('.offerList-item-description-title') as $title)
{
foreach($element -> find('text') as $text){
echo $text;
}
}
But just get returned an empty string, any suggestions?
Thanks.
If you are aware your HTML returned by your scraper does not contain Javascript rendered code, like in your case text is generated by javascript that's why you are getting empty response. What you need is a headless browser like PhantomJS you can use PHP wrapper of PhantomJS http://jonnnnyw.github.io/php-phantomjs/.
This will solve your problem. It has following features:
Load webpages through the PhantomJS headless browser
View detailed response data including page content, headers, status
code etc.
Handle redirects
View javascript console errors
Hope this helps.
I'm not sure what code your using in your example (and I suspect the getContents function result gets in the way of your method for retrieving the text) but if you wrap the text you're after in a <span> like so:
<div class="offerList-item-description">
<div id="result-title-5" class="offerList-item-description-title">
<script type="text/javascript">
document.write(getContents('wF8UD9Jj8:6D !FC6 q23J (:A6D c I ec A6C A24\<'));
</script><span>Huggies Pure Baby Wipes 4 x 64 per pack</span>
</div>
</div>
you can retrieve it using javascript:
<script>
var $title = document.getElementsByClassName("offerList-item-description-title");
for (var i = 0; i < $title.length; i++) {
var span = $title[i].getElementsByTagName("span");
var $text = span[0].innerText || span[0].textContent;
//echo $text;
console.log("==> " + $text);
}
</script>
i need to display as text on the screen a snippet that contains some php variables, but when i load the page Laravel resolve this variables and give me a undefined variable error. Basically i need that when i try to display the snippet inside <code></code> tags it print something like this:
<script type='text/javascript>
var variable = '<?= $variable ?>';
</script>
I have tried already with {{htmlspecialchars}} but not work
In your controller you can assign the code to a variable :
$var = Blade::compileString('<script type=\'text/javascript\'>
var variable = \'<?= $variable ?>\';
</script>');
In your blade, access the variable as :
{{ $var }}
You can use highlight_string($code) to show the snippet. Inspiration from this post: How do I display PHP code in HTML?
Maybe you could try to enclose your snippet on a #verbatim block. This should escape its content of the rendering.
See more here: https://laravel.com/docs/5.5/blade#displaying-data
I have the following script, which returns 2 values from my database.
$(document).ready(function() {
<?
$link=new mysqli($serveur,$login,$pass,$base);
mysqli_set_charset($link, "utf8");
$r=mysqli_query($link, "select sujet,corps from mail where type='Création_rationnaire'");
while($row = mysqli_fetch_array($r)) {
?>
document.getElementById("sujet").value="<? echo $row[sujet];?>";
document.getElementById("te").value="<? echo $row[corps];?>";
<? }mysqli_close($link); ?>
});
Here are my 2 forms, one is a classic Input, the other is a textarea.
<input id='sujet' class="form-field">
<textarea id="te" class="textarea" rows="9" cols="70"></textarea>
The problem is : my SQL request works in MySQL, but when "corps" is echoed as above, nothing appears in the Inputs, like the script was crashing... Only "sujet" can be echoed successfully. So... why, just why ? Thanks in advance for whoever helps me.
EDIT : all I can echo in the "te" textbox is a single string like this : echo 'something';... but nothing else works...
Try this
document.getElementById("sujet").value="<? echo $row['sujet'];?>";
document.getElementById("te").text="<? echo $row['corps'];?>";
or if you use jquery
var message = <?php echo $row['corps']; ?>;
$("#te").val(message);
Textarea does not have a value but it does have text.
in an input box you would have <input type="text" value="example" /> and in a text area the value is inside the element like so
<textarea>example</textarea>
A other way is to use innerHtml (javascript) or html (jquery) .
But note that html tags will be display with one of these options
document.getElementById("te").innerHTML="<? echo $row['corps'];?>";
<textarea> doesn't have value property. Since you are using jQuery to bind document ready, you can use jQuery to set the value.
$("#sujet").val("<? echo $row[sujet];?>");
$("#te").html("<? echo $row[corps];?>");
What is the value of $row[corps]? Is it empty string? Does it contain double quote, or newline characters? If it contains any of them, it will break your javascript source code. What does javascript source code look like when you check source code inside the browser?
It works by inserting the values directly into the plain html forms. I usually use this javascript function because I have tons of forms to fill in my program, and it never failed until now... So my function above was good, but we still don't know why it failed in this particular case...
Problem solved anyways... thanks to everyone.
I created a component using Component Creator for Joomla 3.X
The problem is that on the site the tooltips show html code. For example:
<strong>Title</strong><br/>Description
Instead, on the administrator they are displayed properly:
Title
Description
I was reviewing the documentation here and here, and it seems that it is possible to define the output format when calling the tooltip function but there are no calls inside the component to that function. The only call I see is JHtml::_('behavior.tooltip'); at the begining of the view file and don't know how to specify the output format.
The site is in this URL:
http://50.87.185.99/colombianadederecho_blank/index.php/administradores-ph/registrarse
Added:
It seems like the code is called when building the label for each field:
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('document'); ?></div>
<div class="controls"><?php echo $this->form->getInput('document'); ?></div>
</div>
And it is stored in the title attribute:
title="<strong>Title</strong><br />Description"
So, I think the problem is in the Javascript function, maybe using a .text() jQuery function instead of a .html().
Inspecting the source code I found this, but don't understand why it is not working properly:
window.addEvent('domready', function() {
$$('.hasTip').each(function(el) {
var title = el.get('title');
if (title) {
var parts = title.split('::', 2);
el.store('tip:title', parts[0]);
el.store('tip:text', parts[1]);
}
});
var JTooltips = new Tips($$('.hasTip'), {"maxTitleChars": 50,"fixed": false});
});
jQuery(document).ready(function() {
jQuery('.hasTooltip').tooltip({"html": true,"container": "body"});
});
Solution:
As #ilias found it was a problem between libraries. I had to disable the bootstrap call in the header using this plugin and call it from the end of the body:
<script src="/site/media/jui/js/bootstrap.min.js" type="text/javascript"></script>
I don't know the code that Component Creator produces, however I assume that the tooltips are like in the administrator views. If this is the case you should check for something like this:
<button type="submit" class="btn hasTooltip" title="<?php echo JHtml::tooltipText('JSEARCH_FILTER_SUBMIT'); ?>"><i class="icon-search"></i></button>
and enclose the contents of JHtml::tootlipText() in strip_tags().
Otherwise try to find the line <strong>Title</strong><br/>Description where the tags are shown and enclose it in strip_tags(). For example you might have something that looks like:
echo $this->escape($item->description);
that should be turned into:
echo $this->escape(strip_tags($item->description));