I am trying to change the image_list_url of tiny_mce to php file.
I changed the url to image_list.php file. It generated the exact output text same as the js file.
But even after giving same output it doesn't show's the image list.
I am wondering if the content-type is affecting it or not?
my JS file content:
// This list may be created by a server logic page PHP/ASP/ASPX/JSP in some backend system.
// There images will be displayed as a dropdown in all image dialogs if the "external_link_image_url"
// option is defined in TinyMCE init.
var tinyMCEImageList = new Array(
// Name, URL
["Logo 1", "media/logo.jpg"],
["Logo 2 Over", "media/logo_over.jpg"]
);
my PHP COde:
<?php
require('../../../system/config.php');
$strPath = APP_ROOT.DS.'sys_uploads/images/';
$objFileList = dir( $strPath );
$arrFileList = array();
while (false !== ($entry = $objFileList->read())) {
if( is_file( $strPath.$entry) )
$arrFileList[] = array($entry, ABS_URL.'/sys_uploads/images/'.$entry);
}
$objFileList->close();
header('Content-type: application/x-javascript');
//header('Content-type: text');
?>
// This list may be created by a server logic page PHP/ASP/ASPX/JSP in some backend system.
// There images will be displayed as a dropdown in all image dialogs if the "external_link_image_url"
// option is defined in TinyMCE init.
var tinyMCEImageList = new Array(
// Name, URL
<?php
if( count( $arrFileList )>0 )
foreach( $arrFileList as $dataRow ):
?>
["<?php echo $dataRow[0];?>", "<?php echo $dataRow[1];?>"],
<?php endforeach; ?>
);
my PHP Output:
// This list may be created by a server logic page PHP/ASP/ASPX/JSP in some backend system.
// There images will be displayed as a dropdown in all image dialogs if the "external_link_image_url"
// option is defined in TinyMCE init.
alert('test working or not');
var tinyMCEImageList = new Array(
// Name, URL
["Logo 1", "media/logo.jpg"],
["Logo 2 Over", "media/logo_over.jpg"]
);
Edit:
As per suggestion i even added a popup message too which even didn't show up.
Solution:
dun know what was error on my code but found good solution from link suggested:
http://tinymce.moxiecode.com/wiki.php/Configuration%3aexternal_image_list_url
As both the .js and your PHP file outputs are identical, there should be no difference. text/javascript is the most widely supported mime type for JS, so using that might help.
It would also not hurt to name your dynamically generated JS files using a convention such as XYZ.php.js and using mod_rewrite to parse the php.js files as php.
Edit:
Also, per official TinyMCE docs, please make sure that there is no whitespace before the <?php opening tag in the dynamically generated JS, also check for UTF8 BOM which can be a sneaky cause of invisible output.
No need to change any headers. Just output the JavaScript.
js.php: alert("Working!")
test.htm: <script type="text/javascript" src="js.php"></script>
When I loaded test.htm, I got an alert box
This is definitely a problem of incorrect header type.
could you please change the line
header('Content-type: application/x-javascript');
to
header('Content-type: application/javascript');
As application/x-javascript is not a correct javascript header. Tell me if this thing helps
Related
i'm using sencha Extjs 6.0.2 in the forntend and mongodb in the backend. But, whenever i try to add api.php file in index.html file using script tag, it throws some exception. In index.html i add the following code for api.php file inclusion.
<script src="php/api.php" type="text/javascript" ></script>
the Error is
Uncaught SyntaxError: Unexpected token < api.php?_dc=1468481913442:1
the problem that i found during debugging is that api.php file is called as html type. As far as i know php file should call as a php type, but it doesn't. some times when i resend the file respone using firebug, it type turned into xml. How can i get rid of this problem?
And the error i found after inspecting is below:
Here is my api.php code
<?php
require('config.php');
//header('Content-Type: text/javascript');
header("content-type: application/x-javascript");
$API = get_extdirect_api('api');
// convert API config to Ext Direct spec
$actions = array();
foreach($API as $aname=>&$a){
$methods = array();
foreach($a['methods'] as $mname=>&$m){
if (isset($m['len'])) {
$md = array(
'name'=>$mname,
'len'=>$m['len']
);
} else {
$md = array(
'name'=>$mname,
'params'=>$m['params']
);
}
if(isset($m['formHandler']) && $m['formHandler']){
$md['formHandler'] = true;
}
if (isset($m['metadata'])) {
$md['metadata'] = $m['metadata'];
}
$methods[] = $md;
}
$actions[$aname] = $methods;
}
$cfg = array(
'url'=>'php/router.php',
'type'=>'remoting',
'actions'=>$actions
);
echo 'var Ext = Ext || {}; Ext.REMOTING_API = ';
echo json_encode($cfg);
echo ';';
// document.getElementById("demo").innerHTML = "Hello JavaScript";
?>
i try to send the api.php file type as JS file.
Thanks in advance
I am too new to write comments so I'll just write here I'm not sure if it is helpful but: What are you using to process the php file?
I had a similar problem while using the web server that the sencha cmd provide. This is just a basic HTTP webserver and doesn't support php so you get back the php code instead of the javascript that the php code is supposed to produce.
I've got this piece of code, which should fetch the source code of the website.
$homepage = file_get_contents('http://homepage.com');
echo $homepage;
Instead of actually giving me the source code.. It's shows me the page that I'm trying to get the source code from.
Either use htmlentities or change the content type.
$homepage = file_get_contents('http://homepage.com');
echo htmlentities($homepage);
or
header('Content-type: text/plain');
$homepage = file_get_contents('http://homepage.com/');
echo $homepage;
Try this, using htmlspecialchars:
$homepage = file_get_contents('http://homepage.com');
echo htmlspecialchars($homepage);
That's because you're fetching the source code and (re)outputting it. Your page is just mirroring http://homepage.com.
To see the actual page source, add a Content-Type header before your echo statement:
header('Content-type: text/plain');
This tells the browser treat the source as plain text and not interpret it as HTML.
It's because you are printing the source, your browser will interpret that as a webpage. To see the actual code use:
echo htmlspecialchars($homepage);
I am having PHP code inside my JavaScript file, which is processed by the server by setting the handler for the .js file extension to php5-script using a .htaccess. I tried opening the .js file in Aptana Studio using PHP Source Editor and JavaScript Source Editor, however, both editors highlight the php code as syntax error. I couldn't find an option in the settings where I can set the file type to a mix of PHP and JavaScript, or disable syntax error highlighting for PHP code in JavaScript, or anything else.
I have found a similar question but this is just related to the PHPStorm IDE which I do not want to use.
The content of my .js file currently looks like this:
<?php
header('Content-type: application/javascript');
$app_base_path = $_GET['_app-base-path'];
$app_route = $_GET['_app-route'];
?>
$(function() {
var APP = {
// syntax errors are thrown here
// the single quotes could be escaped to reduce syntax errors
// looking for a way to make a code like this possible without any syntax errors
base_path: '<?= $_GET['_app-base-path'] ?>',
route: '<?= $_GET['_app-route'] ?>'
};
$('body').append('APP.base_path: ' + APP.base_path + ' --- APP.route: ' + APP.route);
});
I previously read your question wrong. My bad. If php is NOT parsing the <?= ?> tags, make sure it's enabled in php.ini
Unless the string data in the $_GET parameters contain strings with single quotes in them, you shouldn't have an issue.
I'm working on a project that has the following problem:
The database gives(via php) an array with a list of JavaScript files that need to be loaded. it is stored in the variable $array(php). I need to extract those source files with php(foreach loop) and load them via JavaScript. It is like this:
document.write("<?php foreach($js_files as $filename) {if( trim($filename) <> "" ){echo '<script type=\'text/javascript\' src=$filename></script> \n';}} ?> ");
The problem is that it loads a couple of files but goes wrong with the first one(a google api file). Does anyone have a sollution to this? Or any ideas in which direction i have to look.
This is a bad idea on multiple levels, but can be fairly easily resolved. But first:
1) Never use document.write(). To dynamically load a script it is better to use:
var script = document.createElement('script'); //create a script element
script.src = 'javascript.js'; //path to src file
//now get the body element and append the new script element
document.getElementsByTagName('body')[0].appendChild(script);
2) Loading scripts like this will likely not work if they need to be loaded in a particular order, as the downloading of dynamic scripts occurs asynchronously (non-deterministic)
3) Generally speaking, you should concatenate your js files to reduce http requests, you can use a tool like grunt to make an automatic build process.
4) If you really, really want those scripts to be dynamically loaded though, you can use the process I outlined in (1) to get the file names use ajax:
//depends on jQuery, but could be written vanilla if needed
$.get('myPhp.php', function(resp){
var arr = resp.split(','); //splits returned string on the comma
var i = arr.length;
//iterate through the results set
while (i--) {
//do process from (1) above
}
});
Note that this will still have the unordered problem. If you need sequential dynamic loading check out something like require.js
If the code needs to be fired from a JavaScript function then you can get rid of the document.write, in favor of creating new script objects, and appending to the head tag.
<?php
foreach($js_files as $filename){
if(trim($filename) != ''){
echo 'var s = document.createElement("script");';
echo 's.type = "text/javascript";';
echo 's.src = "' . $filename . '";';
echo 'document.getElementsByTagName("head")[0].appendChild(s);'
}
}
?>
You have to escape the code "" in if( trim($filename) <> "" ) because the " breaks the javascript string.
I have a javascript file called in my webpage. It contains only one var :
var tab = [ "img1.jpg" ,
"img2.jpg" ,
.......... ,
"img100.jpg"
]
"img_i_.jpg" in tab are from my data base.
I've written a php code to update this file.
...
$res = mysql_query($sql) or die(mysql_error());
$script="var tab= [\n";
while($r = mysql_fetch_assoc($res) )
$script .= "\t\"" . $r['name']"\",\n";
$script = $script."];\n";
$fileName = fopen("js/tab.js", "w");
fwrite($fileName, $script);
fclose($fileName);
...
When I check out my js file, all is correctly written, but when I reload my page, nothing has changed.
Curiously, when I edit that js file manually, and then save it then my webpage changes after reload.
Why ? And how to solve it ? Thanks
You might be writing your file in a different location. Check the current directory under which the php page is run, and try to specify an absolute path to fopen rather than a relative one.
dirname(realpath(__FILE__)) will return the path to the folder where the current php file is stored, so you can generate absolute paths through that.
Edit: I'd also suggest doing some error checking around fopen, so that you know if the file was really opened.
Extra
You should really change your mysql_* calls to at least mysqli_* or PDO. The original mysql extension is old, doesn't support prepared statements and has been deprecated (and is due for removal).