Call php echo from jQuery? - php

I have a jQuery script in my header that calls a php file.
The php file contains data pulled from a remote xspf file, and the output presented in jQuery for refresh purpose. i have 2 echo rules, one for title and one for listeners. right now jQuery calls the file itself which makes the output appear together on a single line.
how can i make jQuery present these echoes separately so i can control their design (CSS)?
PHP:
<?php
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$xml = simplexml_load_file("http://mysitehere:8000/live.xspf");
foreach ($xml->trackList->track as $data) {
$radio = $data->location;
$song = $data->title;
$info = $data->listeners;
}
echo $song;
echo $info;
?>
jQuery:
<script language="javascript">
jQuery(document).ready(function(){
//Carga al comienzo
jQuery('#salida').load('reader.php');
setInterval(function() {
jQuery('#salida').load('reader.php');
}, 2000);
})
</script>
<div id=salida> </div>

Put the echoed info into separate elements
echo '<div class="Song">'.$song.'</div>';
echo '<div class="Info">'.$info.'</div>';
then setup classes in your css for Song, Info (or whatever names you want to give them)

Related

Force echo output to Browser before the PHP script loads

I want to output a "Please wait" message before a PHP script runs but it is output at the same time together with the other echo messages.
The purpose is to inform the user that the page is loading and that one is not tempted to think that the page is not loading.
MRE:
header('Content-type: text/html; charset=utf-8');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: Mon, 24 Sep 2012 04:00:00 GMT");
ob_start();
echo "Please wait";
ob_flush();
flush();
sleep(1);
run_a_function();
function run_a_function() {
// execute some php code, may take some time
echo "progress..";
// execute some php code, may take some time
echo "progress..";
// more..
}

upload file with php

I'm using plupload to upload files to my webapp. I need to upload some xls files but upload fails for some of them. I have no ideea why the upload fails, but I get an error on move_uploaded_file.
I don't now why some upload files are not working. In the following are snippets of my code.
var uploader = new plupload.Uploader({
runtimes: 'html5,flash',
containes: 'plupload',
browse_button: 'browse',
drop_element: 'droparea',
url: 'include/myPlupload.php',
flash_swf_url: 'javascripts/plupload/plupload.flash.swf',
multipart: true,
urlstream_upload: true,
multipart_params:{directory: 'test'},
max_file_size: '15mb',
filters:[
{ title: 'Spreadsheet', extensions: 'xls,xlsx,ods' }
]
});
and parts of my PHP code, looks like this ....
error_reporting(-1);
// HTTP headers for no cache etc
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// ... ...
if(move_uploaded_file($_FILES['file']['tmp_name'], $new_path.$_FILES['file']['name'])){
echo $html = '<div class="uploadedFile">'. basename($v) .'</div>';
} else {
echo 'error on uploading file';
}
The problem is that upload fails on some files only. If I try to upload the same file from another computer that file upload succeeds but upload fails on other files.

Generate a Doc File with an Image

<?php
$docName = "testdoc";
header("Expires: Mon, 26 Jul 2020 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type: application/msword; charset=ISO-8859-1");
header("Content-Disposition: attachment; filename=\"".$docName.".doc");
#/ Create and format your text using HTML, its simple stuff ... trial and error ;)
echo '<img src="logo.gif" />';
?>
Soumya Sarkar
Hi I got this following code from google to generate a Doc file by php. But I am unable to put any image there. Can anybody help me with it?
It requires the link, you will need to host the images online in order for the image to be shown on the doc.
Try this and it is working:-
echo "<img src='http://i.imgur.com/MJ5Sa.jpg' />";

How to update jQuery Progress bar according to a value at the session variable in PHP

I need to fill the progress bar according to the current value of a session variable. The value will be not be constant and is increasing.
How to make it load by itself?
<meta charset="utf-8">
<script>
$(function() {
$( "#progressbar" ).progressbar({
value: 80
});
});
</script>
<div class="demo">
<div id="progressbar"></div>
</div>
PHP
...
// disable caching
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Expires: Mon, 26 Jul 1991 05:00:00 GMT'); // disable IE caching
header('Content-Type: text/plain; charset=utf-8');
echo progressbar_value;
exit();
...
JS
var refresh_period = 1000; // ms
var script_path = '/script.php';
function updateProgressBar () {
$.ajax({url: script_path, success: function (value) {
$('#progressbar').progressbar(value);
if (data.value == 100) {
clearInterval(interval);
}
}
}
var interval = setInterval('updateProgressBar()', refresh_period);
You'll need to use AJAX to talk to a PHP file that echos the session var. It can then update the progress bar.

create javascript code in a php file - SAFARI/CHROME doesn't work

I've created a .php file that write out js code like that:
<?
//my_js.php
// javascript header
header('Content-type: text/javascript');
// Date in the past
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
// always modified
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
// HTTP/1.1
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
?>
//
// js code here
//
then i include the script above in my index.php file like this:
<script type="text/javascript" src="my_js.php?id=<? echo $id ?>">
</script>
This works perfect in Firefox, but SAFARI and CHROME doesn't include my_js.php file at all!
What i'm doing wrong?
**edit:
this is the rendered html in index.php:
<script type="text/javascript" src="my_js.php?id=new"></script>
and this is the my_js.php code:
(it's a very big file so i write out only the first few lines)
var g = { sitepath: "myNullUrl" }
function getBrowserWidth(){
if (window.innerWidth){
return window.innerWidth;}
else if (document.documentElement && document.documentElement.clientWidth != 0){
return document.documentElement.clientWidth; }
else if (document.body){return document.body.clientWidth;}
return 0;
}
that's a strange problem 'cos while i'm viewing source code from Crome/Safari i can access the js file and it seems to be error free!
I'm using Chrome 6.04 and Safari 5, both for mac.
It may be because it is expecting the file-extension to be my_js.js. If this is the case, save your PHP file as my_js.js, then, assuming you're using Apache, use the Apache Directive: ForceType:
Like so:
<Location /your/path/my_js.js>
ForceType application/x-httpd-php
</Location>
Good luck!
Maybe set Content-Disposition: inline; header?
<?php
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
header("Content-type: text/javascript");
header("Content-Disposition: inline; filename=\"download.js\"");
header("Content-Length: ".filesize("my-file.js"));
} else {
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"download.js\"");
header("Content-Length: ".filesize("my-file.js"));
}
header("Expires: Fri, 01 Jan 2010 05:00:00 GMT");
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
header("Cache-Control: no-cache");
header("Pragma: no-cache");
}
include("my-file.js
");
?>
It should work. Or you can change this:
header('Content-type: text/javascript');
to this
header('Content-type: application/javascript');
Note:
application/javascript: JavaScript; Defined in RFC 4329 but not accepted in IE 8 or earlier
text/javascript is allowed in HTML 4 and 5 and, unlike application/javascript, has cross-browser support

Categories