How to run PHP editable in an iframe with JS? - javascript

I created an HTML/JSS editor using this tool, which, when editing displays the result in an <iframe> (preview).
editor.getSession().addEventListener('change', function () {
iframe.contentWindow.document.body.innerHTML = editor.getSession().getValue(); // ex: '<div>Hi!</div>'
});
This is easy, the problem is with the PHP editor... obviously if I insert a PHP code in the innerHTML this can not be executed on the client side.
If value is:
<?php
$name= 'John Doe';
echo $name;
?>
This shows:
<!--?php $name= 'John Doe'; echo $name; ?-->
What is the best way to save this temporary code and run instantly on the iframe (preview)?

What do you mean by
run instantly on the iframe (preview)
If you wanted the user edited php code to run and show output completely in the client side browser,
then there is no pragmatic way to do this.
Because, to run php codes,
you need a php runtime at the first place.
Now, whether the clients have php installed in their system(PC) or not,
you have no access to that via the browser.
Now if you are desperate to run the code( on your server then ) and send output to the clients browser,
then
WARNING:: It's highly dangerous,
taking string's from unknown(and hence untrusted) sources and running them as php code on the server, you can get your server hacked(and whacked) easily at a short time :p
because,you don't know what code they are writing(unless you explicitly moderate it before running :p )
(Now If you don't know what you are doing)
you can use the eval construct as described here:
http://php.net/manual/en/function.eval.php
eval("?> $str <?php ");

Related

Redirect Using JavaScript and PHP

I am using below code for redirect users.
<?php
$redirect_to = "https://google.com";
$redirect = "https://yahoo.com";
echo "<script>location.href = '".$redirect_to."';</script>";
header("Location: $redirect");
exit();
?>
I prefer to use javascript redirect, but in somecase when user have not javascript enabled in his browser, I am using PHP redirect as backup redirect but sometime I am getting header already sent error on php redirect code and sometime I am not getting that error.
if I use exit() after redirect by javascript, I will not able to
redirect user if he have javascript disabled in his browser.
my question is there any way to stop php code if javascript redirect was success? I am not getting idea what I have to do for handle my situation.
Let me know if any expert can help me to solve puzzle.
Thanks!
Well, you are a web developer using PHP and Javascript.
One thing to remember, PHP code always runs first on the server. It's a server-side language, so no matter how you arrange your lines of code, your php code will finish running. Then, the client source code (HTML, CSS, JavaScript) is sent to the browser and executed on it (Client-side).
And next, the job of redirecting is always on the client side, the server doesn't actually redirect people to another site. What actually happens is that your php source code generates a redirect command that sends the browser for a redirect.
Another thing, in php, any work related to the header() function needs to be executed before any content is generated. That is, it must run at the top, before any method echo, exit, print,... or content block.
When deploying a php application, if you already use header("Location: ....") then it makes no sense to use javascript directives, as the browser will prioritize handling the redirect in the header first! !
The source code should really be:
<?php
$redirect = "https://yahoo.com";
header("Location: $redirect");
?>
Another way if you still prefer using javascript redirects and are compatible with javascript disabled, that is to use the tag meta[http-equiv="refresh"]. Refer to the following example:
<?php
$redirect_to = "https://google.com";
?>
<meta http-equiv="refresh" content="5;URL='<?php echo $redirect_to; ?>'" />
<script>location.href = "<?php echo $redirect_to; ?>";</script>
The number 5 appears in the content of the meta specifying the countdown (seconds) the browser will redirect when the page has finished loading. When the page cannot execute javascript, after 5 seconds the page will redirect.

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.

Transferring data from PHP to Javascript for processing [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 6 years ago.
I have a variable set in the PHP with some relevant information that I need to process with the Javascript, but I can't figure out how to port that information over to the Javascript.
I first create a test variable in one javascript file, x
Test='<--!TestString-->'
In the PHP I use str_replace to alter the value of the variable with the relevant data
str_replace('<--!TestString-->',seralize($data), x)
Now I try to see if the data has been added in a different file using alert(Test)
and it shows me <--!TestString-->
Is there a better way to do this and have the data show with the alert function so I can start processing it?
I don't understand your approach at all.
Of course the ideal way to accomplish is to use AJAX to grab data from the server and get into the client side so you can work with it in Javascript. (You can Google "Ajax php and javascript")
The easiest way to accomplish this is to just print the variable in PHP but in a context that it will be available to JS in the client.
<html>
<body>
<script>
var x = <?php echo json_encode($phpvar); ?>;
console.log(x);
</script>
</body>
</html>
I won't argue with anyone that disapproves of that approach but its easy and it works.
Use <script> tag?
<script> my_value = 'some'; </script>
Use AJAX?
// with jQuery:
$.get('/data.php',function(data){/*...*/})
PHP is executed on your server, Javascript in the client (ie. Browser of the user visiting your website). So PHP can not change variables of your JS code once the page has been sent to the browser. But what you can do is set the variable in PHP and insert it in your JS code like this:
<?php
$test="some important info here";
?>
<script>
alert("<?php echo $test ?>");
</script>
If you need to change the variable by the server, you would need to use AJAX to load the new value.
As mentioned before we might not be understanding your approach to your problem.
PHP is executed on the server side while Javascript in the client. PHP cannot change variables of your Javascript code once the page has been sent to the browser. What must be done is to invoke PHP by using preferably AJAX with jQuery.
Look into AJAX PHP with JSON. Perhaps you are looking into the json_encode(value) function?

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

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.

How do you add dynamic anchor text in wordpress links.

We would like to have links in a word press site that have the current meta description of the target site as the anchor text of the link.
I understand this requires either javascript or php and am not sure which is the appropriate approach and which is most easily supported within word press.
If you have Wordpress then you should have cURL installed and activated (or find the way). Also, there is a PHP function called get_meta_tags(). So, you could do something like this assuming you have an array of links with each URL called $links_array:
foreach($links_array as $link){
$tags = get_meta_tags($link);
$description = #$tags['description'];
//Printing each link
echo "<a href='$link'>$description</a>";
}
Interesting question and yes it is possible. You can't do it with javascript or AJAX because the browsers' cross-domain policy won't allow you to do this. I think it has to be a combination of both.
The first solution that i can think of is creating some kind of proxy with PHP, that returns the contents of the targeted URL (the one you link to):
<?php
$url=$_POST['url'];
if($url!="")
echo file_get_contents($url);
?>
Lets say we call this little script "getit.php". Now you can get a AJAX call going, that sends the target url to your .php file and the .php file returns the content of the targeted page. Then you are going to extract the description meta-tag from the returned data.
Of course you could get it in the PHP file and only return the meta description, because that would even be a better solution. You could try something like this in the PHP:
<?php
$url=$_POST['url'];
$tags = get_meta_tags($url);
return $tags['description'];
?>
PS. Apologies for my bad English, it's not my native language.

Categories

Resources