How to show php file output in span by document write? - javascript

My JavaScript code inserts values into a database.
I want to:
echo some value in insert.php file, and
show the results in above span by document.write().
preferable a text link or image link.
What amendment do I need to make to insert.php to do so?
<script language="JavaScript" >
var bhs = document.createElement('script');
var bhs_id = "yvw3lwc1tnvq670ybzprm8xyh93rider";
bhs.src = "//online.examaim.com/insert.php?site=" + bhs_id + "";
document.head.appendChild(bhs);
document.write("<span id='o_" + bhs_id + "'></span>");
</script>

Could you provide more of an explanation as to what you are trying to achieve? It sounds like you might be confused as to how php works but to do specifically what you are asking:
document.write("<span id='o_" + <?php echo $someVariable ?> + "'></span>");
but I am guessing that the file with this javascript in is not insert.php?
You link to php files the same way you link to a html file. I think there may be a fundamental misunderstanding on how php works here: http://www.w3schools.com/php/

Related

VSCode doesn't notice php contained within a JS section of a HTML file

I'm working on a project which uses PHP within JS. However, when I'm working on it, VSCode doesn't seem to realise that it's PHP, and sends out errors. Does anyone know why this might be the case? I don't believe that it has anything to do with the code itself, but rather VSCode not realising that it's PHP. Any help would be great!
PS: All this code is within a html file, and is set up like this:
$urls = file_get_contents('txtfile', FILE_IGNORE_NEW_LINES);
$random = $urls[mt_random(0, count($urls) - 1)];
?>
<script type="text/javascript">
function randsite($random){
<?php
echo "var href = '" + $random + "';"
?>
window.location.href = "http://"+href;
}
</script>
<h5> → <button onclick="randsite();"> PLEASE! </button> ← </h5>
Change language mode:
Command: Ctrl+Shift+P
Select PHP as your programming language.
And be careful with Imports (import correctly)
Install some extensions for intellisense too.

Get a page's source with PHP, manipulate with JavaScript

JavaScript cannot get an arbitrary page's source code, last I knew. But PHP can pretty easliy.
//get page source code with php
<?php
url = 'http://www.thesaurus.com/browse/strong?s=t';
$src = file_get_contents($url);
?>
PHP is not good at manipulating the DOM, but jQuery is great for that!
I would like to do something like
//manipulate source code with javascript
<script>
html = '"' + <?php echo $src;?> + '"';
listItems = $(html + " li");
printLists = '';
$.each(listItems, function(ind, el) {
printLists += el.innerHTML + "<br/>";
});
document.write(printLists);
</script>
But, any time I echo $src into the script tag, it gets interpreted as HTML immediately and the page becomes a live mockery of the actual site.
//Actually just shows me thesaurus.com#strong
<body>
<div id="holder" style="display: none;"></div>
<script>
holder = $("#holder");
nodeNames = [];
html = $.parseHTML(<?php echo $src;?>, holder, false);
</script>
</body>
The phrase 'virtual DOM' sounds right, though I really don't want any of the copied source code to show up at all. I just want to extract certain parts of it : to run a script from the console, search a few thesaurus sites for a term, take the results, and save them to JSON accessed by a local thesaurus script.
I have a solid idea of how to do everything else, didn't expect this to be the tricky part!
Any suggestions on preventing the browser from parsing HTML?
(I would prefer this to run just as a script file without a browser anyway, but had trouble loading jQuery in a thesaurus.js file.)
You could run a php script to get file contents and echo the results to a textarea with readonly/disabled on, then query that php file through ajax to display the resulting textarea on the page.
For example, output.php:
<?php
$str = '<p>I am a paragraph.</p>';
echo '<textarea readonly="readonly">'.$str.'</textarea>';
?>
AJAX call in the original file:
$.ajax({url: 'output.php', success: function(data) { $('#result').html(data); }});

Insert code with JS just like echo in php

I want to get some code snippets from a PHP server to be "injected" in HTML. Basically when I open page.php I get some text like "_off".
The injection works with this code, however I can't use php in this html since it is local.
<img src="images/test
<?php
{ echo "_off"; }
?>
.jpg">
JavaScript seems to be the logical step. But if I just enter this at the position where I want the text of course it doesn't work:
<script type="text/javascript"> $.get( "page.php", function( data ) { document.write(data); } ); </script>
Any ideas?
You need to use $('#someElement').html(data) or $('#someElement').text(data) if you want to write the data to a specific place on the page. You should avoid using document.write
Here is a fiddle to demonstrate: https://jsfiddle.net/yd9mx4d3/

Loading a HTML file into ACE Editor PRE Tag

There is a (on the server) locally stored HTML file which i need to show to user and allow use to make changes to it and save it. (Something like template file editor in wordpress).
For this I am using ACE Editor.
My javascript code:
$(document).ready(function() {
var editor = ace.edit("editor");
editor.getSession().setMode("ace/mode/html");
editor.setTheme("ace/theme/chrome");
editor.setValue("<?php echo addslashes(file_get_contents("abc.html")); ?>");
editor.gotoLine(1);
});
Code in file abc.html
My Problem: Although I have used addslashes, there are some characters that cause problem.
Isn't there a method to directly supply a file to ACE Editor?
Is there any other such editor which can directly be supplied a file name to open?
EDIT: SOLVED!
Instead of passing file text via setValue() function, I directly printed text within PRE tag
<pre id="editor"><?php echo htmlentities(file_get_contents($input_dir."abc.html")); ?></pre>
It worked.
the correct escaping is
htmlspecialchars(addslashes(file_get_contents("abc.html")));
editor.setValue("<?php echo addslashes(file_get_contents("abc.html")); ?>");
is wrong. abc.html is out of php code. syntax error
editor.setValue('<?php echo addslashes(file_get_contents("abc.html")); ?>');
this could work. have not tested

How can I use a theme option in a JS file?

I'm creating a Wordpress theme and have added an option which allowed users to change a font family using (simplified code): update_option('mytheme_font', $_POST['mytheme_font']);
How do I then get the value of that option in a JS file of the theme? I need it because I'm using Cufon to replace some H1's and H2's. Thanks!
You have four options I suppose.
Output your javascript to your page inside script tags. Easiest
<script>
<?php echo 'var x = 3;' ?>
</script>
Output the variable to your page and then read that from your javascript file. Clunky, but does mean you don't have to create some js globals.
<div id="x" style="display: none;">3</div>
var x = document.getElementById('x').innerHTML();
[Added] - Use AJAX to request the data and parse after page load.
Last and I don't recommend it, but setup php to parse .js files to dynamically produce javascript files. This way you could place a <?php ?> call in your .js files.
You could echo some thing like this in the <head> of the header.php
<?php
$defaultThemeFont = "myDefaultValue";
$userThemeFont = get_option("mytheme_font");
if($defaultThemeFont == NULL)
$userThemeFont = $defaultThemeFont
?>
<script>
<?php echo "var mytheme_font = $userThemeFont;"; ?>
</script>
Now you can access this variable from any JS file

Categories

Resources