Include code in html file in captcha context - javascript

I've a file that needs to have a php include
<? include_once("script.php") ?>
Javascript content
<script>
Some javascript code
</script>
And HTML content:
<some tags>
So that I just have to do code to include in html and everything works as I need in the captcha.
I've tried <link>, <iframe seamless> and <script> but none worked... I need this piece of code to interact with the parent code, just like a regular include... How could I do this?

Related

Refresh PHP code inside DIV with javascript won't work

This question is unique because the error
ReferenceError: $ is not defined comes from the fact that javascript is called in the header but the jQuery is looking inside the parent iframe and don't find it.
I got this page:
review.php
<?php include 'review_core.php'; ?>
<div id="primary" class="content-area col-md-9">
<main id="main" class="post-wrap" role="main">
<?php include 'review_index.php'; ?>
</main><!-- #main -->
</div>
<?php get_sidebar();
get_footer(); ?>
in review_core.php I got some php that fetch folders, images and other things from a S3 bucket, and in review_index.php I got some html and php to show up the data runned by review_core.php inside (a lot) some divs.
The get_sidebar(); just get the wordpress sidebar where I have an Iframe and here is where the problem is coming. In this iFrame is runned a php script (some_php_in_frame.php) that when ends should reload the #primary div, so basically it have to reload review_index.php into it. I'm tring to do it by adding this code at the end of some_php_in_frame.php:
some_php_in_frame.php
<?php [...] ?>
<script>
$(document).ready(function(){
parent.location.href=parent.location.href;
$('#primary').load('review_index.php');
})
</script>
but the refresh don't work and as results in debug I get:
ReferenceError: $ is not defined
Make sure that in the HTML of the page that contains the iFrames, as well as any page that needs to run jQuery in an iFrame imports the jquery library in the <head> section of your HTML document.
The error message on your JavaScript console indicates that this is missing.
For Example:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
$() functions are part of the jQuery Library, and not javascript natively.
The solution was easy:
<script>
$(document).ready(function(){
parent.$(' #primary').load('review_index.php');
})
</script>

Is there any way to make Javascript work in DOMPDF-generated PDFs?

I am currently testing DOMPDF and got it working quite nice for my purposes, including CSS styling, displaying content fetched from a mysql database etc.
Now I tried to use some Javascript, but it doesn't work. I used a very simple script for testing:
HTML somewhere on the page:
<div id='mydiv1' style='width: 100%;height:20px;background:#ddd;'></div>
The JS (just above the closing </body> tag (but I also tried it right after the opening <body> tag):
<script>
document.getElementById('mydiv1').innerHTML = 'this is a test';
</script>
When I echo this page in the browser (I am echoing a variable which contains the complete HTML/PHP page), that text appears in the DIV. When I put the same variable in DOMPDF's loadHtml and then render and output it, the script-generated text doesn't appear in the PDF (the rest of the page does).
So my question is: Is there any way to make Javascript work in DOMPDF-generated PDFs?
Unfortunately, DOMPDF doesn't support javascript. You may consider looking at something like phantomjs, which can be used to save pdf files, as well.
There is a DomPDF option to turn on inline javascript:
$isJavascriptEnabled = true;
Heres an example of how to use the DomPDF options:
$HTML = <<<HTML
!DOCTYPE html>
<html>
<head>
<body>
some html
</body>
<script> somejs </script>
</head>
</html>
HTML;
require_once "sites/all/libraries/dompdf/autoload.inc.php";
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->set('isJavascriptEnabled', TRUE);
$dompdf = new Dompdf($options);
$dompdf->load_html($HTML);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream('blah.pdf');
I took this from DomPDF's options page: https://github.com/dompdf/dompdf/blob/master/src/Options.php

How to include inline JS files with PHP?

Normally that is how one includes JS files in HTML:
<script type="text/javascript" src="myFile.js" ></script>
But I want to write the JS file inline the HTML, using PHP; but still having the possibility of having my neat JS file, that can be chached, debugged and minified.
How can I do something like this?
<script>
<?php include 'myFile.js';?>
</script>
In other words, how to include a JS file in HTML on the server-side. That may get a lot of advantages because it is transparent to the browser.
How can I do something like this?
<script>
<?php include 'myFile.js';?>
</script>
Exactly that code would work, yes. Though it would try to interpret the file as PHP code, and if anything looked like <?php .. ?> inside that file you may see weird side effects. If you want to include the file uninterpreted, simply do:
<script><?php echo file_get_contents('myFile.js'); ?></script>
Be aware of possibly having to escape "</script>" now, should that appear in your included file; see https://stackoverflow.com/a/6908901/476.
YOu need to make custom function or use :
echo '<script type="text/javascript" src="myFile.js" ></script>';
Or
function add_js($filename){
return '<script type="text/javascript" src="'.$filename.'" ></script>';
}
echo add_js('myFile.js');

JavaScript: load file that contain include PHP fucntion in div

I have this situation.
file.html
.......
<div id="mydiv">
</div>
.......
myjsfile.js
............
$("#mydiv").load("myphpfile.html");
............
myphpfile.html
............
<?php
include("myfile.php");
?>
............
The content of myphpfile.php is not viewed. How can I show the content of myphpfile.php in selected div?
Thanks.
You can use php also in load funtion.
$(document).ready(function(e) {
$("#mydiv").load("myfile.php");
});
Above code will load after page loaded
include("myfile.php");
include function will run before loading html and javascript codes..
Use this script file for library jquery-1.9.1.js
We could not able to include PHP file with in HTML file. You have to call php file instead of HTML file.
In PHP, there are many function to include Myfile.php file. please find the include function as below:
include()
require()
include_once()
require_once()

Is it possible to put a javascript function in a php file?

Non-programmer here, trying to figure something out.
I have a javascript function in the header of my document that upon page load it opens another page in an iframe and reveals an svg file on my server for minor online editing. I would like to place my javascript function in a php file, so that these folder locations of the svg files cannot be determined for anyone to download these svg files freely.
Currently I have this on my html header:
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
Since I have heard that php is a server side script and not viewable, I thought this would mask the location of these files on my server.
I want to remove this javascript code from my header and place it in a php file and replace the header code with either of these:
<script src="phpjavafile.php"></script>
or
<?php include ('phpjavafile.php'; ?>
and finally put the javascript into a php file like this:
<?php
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
?>
I have tried both of these methods and neither load properly.
I must be doing something wrong. Am I on the right track, or is there a better way of getting this to work.
Thank you ahead of time.
By using the echo() function
PHP
<?php
echo '<script>
some javascript
</script';
?>
However if you are just trying to load the php on page load without any php args then just write it out of the tags.
If you have the JavaScript is another file then using
PHP
<?php
include 'path/to/javascript/file.php';
?>
Should work.
You cannot hide javascript as it is interpreted browser side and not server side so the users browser has to have accesses to the code.
Here is the code I think you are asking for:
PHP HTML
<?php
$html = file_get_contents("path/to/data.html");
?>
<div>
<?php echo $html; ?>
</div>
doesn't use an iframe but should still work. However any relative links will not work unless both files are in the same dir and there will be no iframe functionality without additional css
You can just output it as text in your PHP file.
<?php
// Some PHP stuff here
?>
<script type="text/javascript">
function loaded(){
....
}
onload=loaded;
</script>
<?php
// Some more PHP stuff here
?>
Alternatively, you can just link to it in the usual way from within the same file:
<script src="path/to/your/file.js"></script>

Categories

Resources