PHP file_get_contents() in setInterval() not working - javascript

I am trying to use get_file_contents() in a setInterval() to repeatedly update some text which is displaying the contents of a file. Here's the code:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<body>
<div id="theDiv"></div>
</body>
<script>
$("document").ready(function(){
setInterval(function(){
$("#theDiv").text("<?php echo file_get_contents('file.txt'); ?>");
console.log("<?php echo file_get_contents('file.txt'); ?>")
},500);
});
</script>
The problem is: it seems that the text is not updating until I reload the page.

the file_get_contents() is NOT set to reload by setInerval. You mixed up client-side JavaScript & server-side PHP.
You probably need the following:
$("document").ready(function(){
setInterval(function(){
$("#theDiv").load('file.txt');
},500);
});

You have to create another php file to output the content and use ajax to get it.
Example:
$("document").ready(function(){
setInterval(function(){
$("#theDiv").load('/echo_the_content.php');
},500);
});
Create echo_the_content.php with:
<?php
echo file_get_contents('file.txt');
If your file.txt is in public dir (web accessible), then you could load it directly without another php file.

the setInterval get executed in the client side where as the php is evaluated in the server side so the command echo file_get_contents('file.txt'); is evalulated only once.
You need to use an ajax request where it returns the contents of the file
$("#theDiv").load("file.txt");
where the server side resource file.txt returns the desired response that has to be displayed in the div

Related

Load php before html page load

I want to first load my php code to get some values ​​and then load my web html with the values ​​already loaded. This is possible? I am looking to do this, since I want to call my php code without using Ajax and at the same time the values ​​obtained by the Php will be loaded in my web html.
Like this:
Load www.example.com -> redirect to www.example.com/index.php
---index.php---
<?php
//do something
include("YourHtmlWeb.html");
echo '<script type="text/javascript">',
'YourFunction("hi");',
'</script>';
exit;
?>
---Html-javascript---
<script type="text/javascript">
function YourFunction(msg){
alert(msg);
}
</script>

How to read jquery cookie value in php cookie value?

I am not sure is it possible to read jquery cookie value in php cookie value?
var current = $.cookie("count");
show<?php echo $keys; ?><?php setcookie("bgx","document.write(current)"); echo $_COOKIE["bgx"]; ?>now.play();
$.cookie('count', <?php echo $count; ?>);
Here is a simple demo that shows how to create a cookie with JavaScript and read it with PHP when you reload the page (because JavaScript is executed after PHP, as it is client-side). Copy this code onto a new PHP document, upload it to your server, and try it.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<h1>Cookie demo</h1>
<p>This demo shows how you can create a cookie with Javascript (using jQuery), and read it with PHP.</p>
<?php
/* THIS PORTION IS EXECUTED ON THE SERVER */
// if the cookie "myCookie" is set
if(isset($_COOKIE['myCookie'])){
echo "<p><b>PHP found this value for <i>myCookie</i>: " . $_COOKIE['myCookie'] . "</b></p>";
}
else{
echo "<p><b>PHP did not find a value for <i>myCookie</i>. Give it a value below.<b></p>";
}
?>
<input type="text" id="myInput"/><button id="myButton">Change the cookie value using JS</button>
<!-- make sure you load jQuery and the jQuery cookie plugin -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
/* THIS PORTION OF CODE IS ONLY EXECUTED WHEN THE USER SEES THE PAGE (CLIENT-SIDE) */
$(function(){
$('#myButton').click(function(){
$.cookie('myCookie', $('#myInput').val());
alert(
'The value of myCookie is now "'
+ $.cookie('myCookie')
+ '". Now, reload the page, PHP should read it correctly.'
);
});
});
</script>
</body>
</html>
A cookie is a piece of data that is stored in a browser and associated with a website. Every time the browser requests something from a website, it includes the cookies in the request headers.
You can read and write cookies with client side JavaScript. (NB: It is possible to mark cookies as http_only, in which case they can't be read with client side JS).
You can read (by examining the request headers) and write (using response headers) cookies with server side code.
The usual rules of timing apply:
show<?php echo $keys; ?><?php setcookie(
Setting a cookie with server side code requires you to issue a response header. PHP doesn't let you issue response headers after you have started outputting the response body.
You are trying to call setcookie too late. You need to call it before you start outputting any content.
"document.write(current)"
I'm not sure what you are trying to achieve here.
It doesn't make much sense to store JavaScript code in a cookie.
You can't generate server side code using client side code, so if this is an attempt to write the value of current into a cookie, then it is too late.
setcookie("bgx","document.write(current)");
echo $_COOKIE["bgx"];
$_COOKIE is populated with data from the request.
It won't have data in it that your current response is about to ask the client to store. You won't get that new value until the next request.
In short: To read a cookie set with JavaScript, you have to make a new HTTP request after you have set it.

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.

How to store the value returned by javascript function into a php variable?

The output generated by the program below is Inner textjain. Fine, but I want $m php variable to be assigned the value returned from JavaScript function a().
<html>
<head></head>
<body>
<?php
$name='abhi';
?>
<div id="a">
</div>
<script type="text/javascript">
function a(){
var a="jain";
<?php echo $GLOBALS['name'] ; ?>=a;
document.getElementById('a').innerHTML="Inner text"+<?php echo $name; ?>;
return <?php echo $GLOBALS['name'];?>;
}
</script>
<?php echo $n='<script type="text/javascript">' , 'a();' , '</script>';
echo $n;?>
</body>
</html>
You can't. Once the page is delivered to the browser, which is where Javascript runs, your PHP server has already shut the request down. If you somehow really want to send something calculated in Javascript back to PHP, you'll have to make an additional request to the browser from Javascript (using AJAX) but even then the variable will be available in a new request, not the old one.
PHP runs on the server, Javascript runs on the browser, and they don't run concurrently, so you'll have to send your Javascript variable to the PHP script (and re-run the PHP script), which involves reloading the page.
You can e.g. a GET request (by loading script.php?yourvariable=value).
you can't. PHP runs at the server first and then javascript will run at client side.

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