Is it possible to edit javascript code so that before it executes on page load I can inject few code changes through PHP. I want to use PHP so that changes happen on the server before page is loaded onto client side.
For example, this is what I have:
<script>
video38.addParm("<param name='FlashVars' value='file=abc&fullscreen=true&width=440&height=241&'>")
</script>
And this is what I want:
<script>
video38.addParm("<param name='FlashVars' value='file=abc&fullscreen=true&width=440&height=241&oneMoreParameter=paramValue'>")
</script>
I tried this with jquery but because this is flash, once it is loaded then adding parameter doesn't do anything. That's why I thought doing it in PHP because it executes on server side.
Any help on this will be great.
Thanks!
Take a look below:
<?php
$test = 'test';
?>
<script type="text/javascript">
var test = '<?php echo $test; ?>';
</script>
Hope, this will be helpful to you!
$ob = ob_start();
//EXECUTE YOUR CODE HERE, THE PAGE AND STUFF e.g. include('./file.html');
$pi = ob_get_contents();
ob_end_clean();
$js = <<<'EOD'
video38.addParm("<param name='FlashVars' value='file=abc&fullscreen=true&width=440&height=241&oneMoreParameter=paramValue'>");
EOD;
$pi = preg_replace("#video38.addParm(.*?);#s", $js, $pi);
echo $pi;
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>
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>
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.
This is a theoretical question.
My question is whether a jQuery function or script can be written inside a PHP function.
E.g.
<?php function phpfunc(){
$a=10; ?>
<script>var a ='<?php $a ?>'</SCRIPT> <?php } ?>
Is this possible and legal?
Yes. It is possible and Legal one too. we generally use the same when we require any server side value to be set on client-side on runtime.
Hope this answers your query.
Thanks Much!
When php code is interpreted by the sever writing something like:
<?php
function foo()
{
<script type=text/javascript> ... </script>
}
?>
As part of the code in <?php ?> is interpreted as php and string inside the function doesnt represent any of php functions
You can echo javascript code (or any content of a HTML document) through your php code like:
<?php
function foo(){
echo "<script type=text/javascript> alert('it works!)'; </script>";
} ?>
so when you execute the function, you wil add the javascript to the document by echoing it and therefore execute it.
You can also use php variables to echo variables to javascript like:
<?php
function foo(){
echo "<script type=text/javascript> alert('{$phpVariable}'); </script>";
} ?>
or
<?php
function foo(){
echo "<script type=text/javascript> var variableFromPHP = {$phpVariable}; </script>";
} ?>
Yes, it's okay to do that. No, it's probably not a good idea. But there's nothing really stopping you.
Just be aware that if your variable happens to have a ' in it, you'll get messed up.
So, whenever you want to pass a variable from PHP to JavaScript, be sure to use json_encode:
<script type="text/javascript">
alert(<?php echo json_encode($something); ?>);
</script>
Note that there's no quotes in the JavaScript part - json_encode will add quotes if needed. This can be used to pass almost any kind of variable.
The short answer is no(edit : yes).
javascript is executed on the client and only on the client.
You can however echo javascript to the client.
So something like this :
$JSfunction = "<script>alert('This is working')</script>";
can be echoed to the page by doing echo $JSfunction;
edit :
Since you didn't mention where that function is located, I assumed you meant the PHP function on the server side.
To be clear, If it's written on the html page itself, it's perfectly fine and can be done.
complete answer
<? function phpfunc(){
$a=10; ?>
<script>var a ='<?php echo $a ?>'</SCRIPT> <?php } ?>
<?php phpfunc() ?>
<script>console.log(a);</script>
You must echo that $a
Yes you may use it like in normal html page. Use script include inside echo inside the php script.
You may use it outside the php script normally. Use container tag for the same in the php page.
I hope it would help.