php var inside javascript inside php - javascript

so I've tried a few things but nothing seems to work. what im trying to do is
<?php
$php_var = a-thing;
echo '
<script text/JavaScript>
document.cookie = "arrayid"+'.$php_var.'+;
</script>
';
?>
so when im not trying to set the array placement '.$php_var.' through just fine. can someone please help me with what I would assume is a syntax error on my part, please

Three things:
(php) You have an erroneous semi-colon ; after $php_var
(js) You need double quotes around the javascript string (if $php_var is a string)
(html) You are missing the type= attribute name in the script tag (you are using the default value for the attribute so you could just use <script>...</script> instead)
<?php
$php_var = a-thing;
echo '
<script type="text/JavaScript">
document.cookie = "arrayid"+"'.$php_var.'";
</script>
';
?>

You can try this: <?=$php_var;?> this will echo the variable in normal html code.
<?php
$php_var = "a-thing";
?>
<script text/JavaScript>
document.cookie = "arrayid"+<?=$php_var;?>;
</script>

Try this:
<?php
$aThing=7;
$php_var = $aThing;
echo '<script>document.cookie ="arrayid'.$php_var.'";</script>';?>
There is not much to explain. The PHP script generates the following string:
<script>document.cookie ="arrayid7";</script>
I left out the text/JavaScript part, as HTML5 does not require this anymore.
See the little demo here: https://rextester.com/HSCMD53489

Related

Word Press PHP ACF Snippets in JS functions

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>

php variable to javascript variable in html file

i have php file which has a variable which is calculated from a given data. i want to pass this variable to a java script variable which is located in other html file can some one help. i have tried like this but when i tried to display the value nothing comes. i want the variable to display countdown timer.
<html>
<body>
<?php
require_once "getuser.php";
?>
<script>
var arrival_time = "<?php echo $arrival_time; ?>";
</script>
<body>
</html
Try making it the php value vs. a string like this:
var arrival_time = <?php echo $arrival_time; ?>;
Well, I don't know if this is a good practice but this is my method of passing a php value to javascript.
<input type="hidden" value="<?php echo $arrivaltime; ?>" id="atime">
<script>
var arrival_time = $("#atime").val();
</script>

How to use a php inside js

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>

display php file content in the javascript of another php file

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.

JavaScript jQuery inside PHP function

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.

Categories

Resources