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>
Related
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
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>
I am trying to print out a variable from a default.js file. That variable counts up to a number, which I want to print on the html page.
So as it is now, I have:
We found <script type="text/javascript">
var $tempCountPlaces;
document.write($tempCountPlaces); </script> places for you!
<div id="places_list">
<script type="text/javascript">
var places_file = '<?php echo site_url().'/places.html'; ?>';
var region_id = <?php echo $region; ?>;
var event_type_id = <?php echo $eventtype; ?>;
var seats = '<?php echo $seats; ?>';
</script>
As you can see, I can send files from PHP to Javascript, and then use these variables in the default.js file. But how do I import a variable from default.js to the html page, and then print the variable, as shown above?
The var $tempCountPlaces; IS defined in the default.js file, and it is not a local variable within a function.
So basicly i want to pass the variable from default.js $tempCountPlaces into my HTML file using the script tag.
You might want to consider approaching the problem like this:
HTML file contains:
We found <span id="numOfPlaces"></span> places for you!
JavaScript file contains:
var tempCountPlaces = 10;//example value fetched from your PHP service
document.getElementById("numOfPlaces").innerText = tempCountPlaces;
//of if you choose to use jQuery: $("#numOfPlaces").text(tempCountPlaces);
Your resulting interface would end up looking like this:
We found 10 places for you!
I use to ask the same question, and yes ajax and axios can help, but if you really need to pass any var from directly php to javascript, or from the script in your html yo you doc.js you have to do this:
<head>
<script type="text/javascript">
var places_file = '<?php echo site_url().'/places.html'; ?>';
</script>
<script src="js/youDoc.js" type="text/javascript"></script>
</head>
As you can see, you have only to declare your vars first and then add your js doc.
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.