Escape <?php ... ?> on javascript file - javascript

I'm busy doing server side and client side validation for magento. this validation works fine on server side (php)
on the client side i am using javasrcript.
When i started on this. i had my javascript embedded on a phtml file and everything was working as expected.
because i am using magento so I decided to inject the javascript file via page.xml
When I added the javascript code instead of getting the message pulled I get the php as is.
Here is my javascript:
function DefaultAddressErrorChangeNotAllowedMessage() {
alert("<?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>");
return;
}
I run this when a user hit the onclick it will point to this function DefaultAddressErrorChangeNotAllowedMessage()
and the
<?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>
will be populated as is.
but when I embed this directly to a phtml file it pull the correct message.
I there a way for javasrcipt that I can use to escape the php and get the correct message which is pulled from config.xml

PHP is rendered server side only. If you need to "inject" PHP specific values to your javascript, then you either need to render the actual value as part of the output of the php script, or you need to take a new roundtrip to the server, using Ajax.

Javascript is clientside, PHP is server side, so all php has been evaluated when javascript is loaded. This means, you can alert php echos, but you can't run PHP operations or any PHP logic in Javascript. You need ajax for this.

sorry for my clumsy answer, but maybe you lost the simple things.
I see that your javascript contains php tag, so i think you should insert your javascript code into .php extension because .js extension can't recognise the php tag.

Related

How do you run a server-side script without it being sent to the browser at all?

I understand that the title is a little confusing, but essentially I want to make my website run a php file on the server side that uses arguments from the html e.g
document.getElementById("example").value;
So I'd like to run it on the server but not have it linked to the html file. Is this possible?
I suggest you create PHP scripts on server side and reach them with XMLHttpRequest (tutorial here) or AJAX with Jquery for example.
PHP scripts you need, don't have to generate any HTML code : they just would process data.
In your JS, you get your values from the page, then you send your data to the server.

How can I execute Ruby code in a textarea and show the results in an iframe securely?

I have a <textarea> which I want to use as a Ruby code editor on my HTML web page. I want to run the Ruby code using Javascript and use a <div> with an ID of output as a command line in an <iframe> to output the results of the Ruby code, just like Codecademy.
Also, I would like to find a way to do this securely so it doesn't compromise the security of my server. I can only use PHP and Node.JS.
If I understood your question right, what you need to do, is to POST whatever is in the <textarea>to a PHP script, which could then, write the POST(ed) code into a file, and use a PHP system call to execute that code.
<?php
file_put_contents('tmp.rb', $_POST['txtTextArea']);
echo system('ruby tmp.rb');
?>
original link to Executing Ruby script from PHP and getting output
.
I would suggest that you do that using an asynchronous JavaScript call such as fetch (or ajax), then you can have the PHP return the result of the Ruby execution, and you can print it somewhere inside your page.
Beware that this opens the possibility to remotely execute commands that can be dangerous depending on the permissions of the web server user configured on the server running PHP and Ruby.

Is there any way to display raw data of a .PHP file with using Java script in my localhost?

I am trying to display the raw contents of a .php file using javascript but i am unable to get it. The php file is in my localhost.
PHP is server code. JS is client code. (Execution talking)
You can't access your PHP files from your .js
The only thing you can do is to get some PHP output using Ajax but I think that is not what you are aiming for.
You can use show_source() function to output the file with the PHP syntax:
<html>
<body>
<?php
show_source("test.php");
?>
</body>
</html>
This function has to parameter show_source(filename,return). filename is required and second parameter is optional if this parameter is set to TRUE, this function will return the highlighted code as a string, instead of printing it out. Default is FALSE
There is no way to use client side code to cause the server to provide the server side source code.
You would need to work on the server to create a URL which provided the data you want.
You could then fetch it using a link, JavaScript's XMLHttpRequest or whatever other method took your fancy.

"Unexpected token <" when using <?php in a Javascript file

Being new to this, I'm trying to pass a variable from PHP to Javascript.
In my php page, I use a test variable:
$testval = "x";
In my .js file:
var exarr = <?php echo json_encode($testval); ?>;
I've tried several things, but it seems I always get Unexpected token < when I include "
What am I doing wrong ?
Thanks !
In order to use PHP code in any file, the web server has to run that file through the PHP processor. This is configured to happen by default with .php files, but not with .js files.
You could configure your server to process .js files as PHP, but that's generally unwise. At the very least, it creates a lot of unnecessary overhead for those files since most of them won't (or shouldn't) have PHP code.
Without knowing more about the structure of what you're trying to accomplish, it's difficult to advise a "best" approach. Options include (but may not be limited to):
Defining that one var on a PHP page which references the JS file, thereby making it available to the JavaScript code.
Putting the value in a page element somewhere that it can be accessed by JavaScript code, either as a form value or perhaps a data value.
Making an AJAX request to the server to get that value (and other values) after the page has been loaded.
If you have that in a js file (like somefile.js) then PHP isn't going to parse that file by default. In the PHP file that links to that JS you can output a script tag and the var you want like:
echo "<script>var exarr = " . json_encode($testval) . "; </script>";
And make sure your script is linked in after that code;
.js files are not compiled by PHP. The easiest workaround is to put the Javascript in a <script> block within a .php, but you're making one of the most basic of serverside/clientside mistakes and should rethink your entire approach.

Remove script tags using javascript or php

The title may not be very clear but I will clearly explain my problem here.
I am designing a website for my institute in which I had to provide a editor where user can create html pages and save them to certain folder (user wouldn't know the exact folder, it's created using php while registration). I have decided to use ck-editor for the editor purpose. To save the data I send a post request using ajax to a php script which simply uses
file_put_contents("folder/file_name.html",$_POST['data'])
To show the pages view_page.php accepts the file name as a get variable and then includes the html file e.g.
URL:
view_post.php?file_path=user/good.html
PHP CODE:
<?php
try{
#include_once("ed423eba62af16d6ab38cbfd2295b304/".$_GET['file_name']);
}
catch(Exception $e){
echo "Can't find the requested file.";
}
?>
Now the problem I am facing is that if user submits data that contains some script tags I have to make sure that the script tags doesn't get saved or doesn't run when the page loads. How can I do that?
You can try strip_tags() - http://www.php.net/strip_tags or HTML Purifier - http://htmlpurifier.org/
I would suggest HTML Purifier.

Categories

Resources