What am I doing ?
I have a dynamically created html division containing the score of a game played by user which user can share on social media.
For sharing I have meta tag e.g. facebook
<meta property="og:image" content= "<?php echo $img_url; ?>"/>
Where $img_url is the link of the screenshot of dynamically created html division.
test.php -- where my above mentioned meta tag is placed and I want to take screenshot of html div if $img_url is not set.
<?php
session_start();
$img_url = $_POST['img_url'];
if(!isset($url)) {
/* <script>
function capture() {
html2canvas will give var img.
Which will be POSTED to testsave.php to save it in the server.
}
</script> */
}
?>
<html>
<!--dynamically created html division -->
</html>
testsave.php -- which will take the posted value from test.php and save the screenshot image to server and will send back the $img_url to test.php
<?php
session_start();
/*Get the base-64 string from data
Decode the string
Save the image
function predefined for random string*/
$img_url = $random_str.".png";
file_put_contents($img_url, $unencodedData);
$_SESSION['img_url'] = $img_url ;
?>
What my problem is ?
When facebook scrapes a paricular page , it doesn't take session values or any values from other page. So I can't send img_val from testsave.php
to another page because scraper won't read POSTED values.
So, what am I doing is when scraper will scrape test.php then if $img_val is not set I want my code to take the screenshot and post js variable var img to testsave.php to save the image, which will then post back the value of $img_val to test.php.
I know there may be many mistakes in above code like I can't put js inside php. But I tried it many way but couldn't figure it out.
Can you please suggest what can I do to make the code work or can you suggest any other way to do it ?
You have to echo out HTML from PHP
<?php
session_start();
$img_url = $_POST['img_url'];
if(!isset($url)) {
echo "<script>
function capture() {
//your js code here
}
</script>";
}
?>
<html>
<!--dynamically created html division -->
</html>
One of the solutions is to use here-document to assign the whole HTML page to PHP variable and later echo/print it where it matches your needs, and you can include the Javascript codes there as well
Example "this example doesn't include the image thing you mentioned but you can use any Javascript codes"
<?php
$variable=<<<_END
<html>
<p>Some HTML line...</p>
<script>
//you can write the Javascript codes you need here
console.log('Find this sentence at console !')
</script>
</html>
_END;
echo $variable;
?>
In order to deliver it in browser, we have to construct JS code as a string inside the PHP code
eg. Test.phtml - where i am calling a js function under some condition.
<body>
<?php if($this->emptyData){ ?>
<div id="someTestDialog">
<!-- call js function below -->
<?php
echo '<script type="text/javascript">testDialog();</script>';
?>
</div>
<?php } ?>
</body>
<script>
function testDialog() {
return $("<div class='dialog' title='Alert'><p>Hello World</p></div>")
.dialog({
resizable: false,
height:90,
width:90,
modal: true,
dialogClass: 'no-close success-dialog',
buttons: {
"OK": function() {
$( this ).dialog( "close" );
}
}
});}
$(document).ready(function() {
var text = "some code here" }); </script>
Related
I'm currently adding in Advanced Custom Fields for my site but when I try and add an ACF snippet to any of the functions the content doesn't show in the browser and all my js code in the script tags stop working and displaying in the browser. what am I doing wrong?
the js code is in script tags in the front-page.php file
function changeTestimonial1(){
document.getElementById("client-name").innerHTML ="<?php the_field('client_name1'); ?>";
}
I would suggest constructing your html/php/javascript as such:
<?php
$somevar = 1234;
?>
<HTML>
<script language="JavaScript">
// JS will contain value of php variable $somevar, but make sure you escape the value.
var some_js_var = <?php echo $somevar; ?>
</script>
</HTML>
I have a below script, which i need to add in a .js file.
jQuery( ".followlink" ).click(function(event){
event.preventDefault();
var $k = jQuery.noConflict();
$k.dialog({
title: ' ',
backgroundDismiss: true,
content : "<?php echo $this->getLayout()->createBlock('catalog/product_list')->setTemplate('catalog/product/list/followup-popup.phtml')->tohtml();?>"
});
$k('.jconfirm-scrollpane .container').addClass('follow-container');
});
I get error marked on this line <?php echo.., can anyone tell how to add this inside the JS file.
I have encloded the entire snippet inside the script tag.
You could do it in one way,
give element property like data-content and then in content label just put $(this).data("content"); so, Code should look like below:
jQuery( ".followlink" ).click(function(event){
event.preventDefault();
var $k = jQuery.noConflict();
$k.dialog({
title: ' ',
backgroundDismiss: true,
content: jQuery( ".followlink" ).data("content"),
});
$k('.jconfirm-scrollpane .container').addClass('follow-container');
});
Normally a javascript file is not interpreted by the PHP engine. You'll need to either modify your server's rules to process that file with PHP, or change the extension to .php. You'll also need to change the resulting content-type before outputting anything:
<?php
header('Content-Type: application/javascript');
?>
You need to serve the JS as PHP OR externalise the PHP:
Do this in your PHP file:
<script>var content = "<?php echo json_encode($this->getLayout()->createBlock('catalog/product_list')->setTemplate('catalog/product/list/followup-popup.phtml')->tohtml());?>"</script>
<script src="yourjsfile.js"></script>
and have content: content in the js file
When i use the php and js separately it is working correctly
<script> var test = 'asdasdasd'; </script> <?php $id =
"<script>document.write(test)</script>"; echo $id; ?>
But when I use the php inside the js function it is not working correctly
<script> var test = 'asdasdasd'; <?php $id =
"<script>document.write(test)</script>"; echo $id; ?> </script>
How can make work my second code?
The second code produces wrong HTML/JS:
<script> var test = 'asdasdasd'; <script>document.write(test)</script> </script>
- see for yourself in the generated page.
Since everything between <script> and </script> is considered to be JS code, it will try to parse the inner <script> as Javascript code...and fail.
I think the inner other of <script>...</script> tags (those that are in PHP markup) is not needed at all, just get rid of them and it will work.
You can insert any value from php to absolutely everywhere in your file. Literally everywhere.
While writing client-side code, in the right place open php tag <?php and echo what you need. Don't forget to write a closing tag ?> afterwards.
Here, you are echoing the opening script tag, which you already wrote before. That results in a syntax error:
<script>
<script>
...
</script>
So basically i want to html() a php file but i seem to fail, maybe it isn't possible but i want to make sure.
This is an example code (not the real one):
page.php:
<? //some code generating html ?>
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<? include('php/content.php'); ?>");
</script>
php/content.php:
<div class='realContent'>Awesome Stuff Here</div>
Note: I know there is .load(), $.post and many others. I just want to know if this practic is possible, .html()-ing a php file's contents.
Thank you in advance, awesome community!
d3nm4k.
The code in my example does what you want.
Maybe you got the problem with the short opening tags that is < ? ? >.
The file I'm trying to load in my example is located in app/View/Tests/def.php and works just fine.
Hope it helped you!
<div class='content'></div>
<?php
$fileLocation = APP . 'View/Tests/def.php';
// Ensure the file exists
echo "FILE EXISTS: <b>" . (file_exists($fileLocation) ? "TRUE" : "FALSE") . "</b><br /><br />";
?>
<?php
// Alternatively, you can try this one
//include $fileLocation;
?>
<script type='text/javascript'>
$('.content').html("<?php include($fileLocation); ?>");
</script>
You can do it, the code you gave i tried it just by adding ?php
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<?php include('php/content.php'); ?>");
</script>
When the php is executed on server it replaces content with actual html inside html() function. And on client side javascript plays its role to render it.
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>