3

I builded my own online javascript editor, but when I submit form, javascript not work. Here are my codes:

editor.php

<form action="show.php" method="post" target="window">
HTML code:<br />
<textarea name="html"></textarea><br />
JavaScript code:
<textarea name="javascript"></textarea><br />
<input type="submit" value="Test javascript" />
</form>
<iframe src="show.php" width="880" height="600" name="window"></iframe>

show.php

<html>
<head>
</head>
<body>
<?php
  if(isset($_POST['html'])) echo $_POST['html'];
  echo "<br><br>";
  if(isset($_POST['javascript'])){
    echo "<script>";
    echo $_POST['js'];
    echo "</script>";
  }
?>
</body>
</html>

HTML works well, but when I say something in javascript textarea, javascript will not work in iframe. Please, help me.

6
  • 1
    What specifically doesn't work? What do you expect to happen? Commented Dec 22, 2012 at 23:02
  • 4
    WARNING! You're passing unfiltered, unvalidated user input right back out. This can create a cross-site scripting vulnerability, easily allowing malicious users to hijack your site for their own purposes. Hell, you're even doing this intentionally! Commented Dec 22, 2012 at 23:03
  • 1
    I want something like jsfiddle.net where you can say your HTML and JavaScript code and you can see output Commented Dec 22, 2012 at 23:06
  • not going to work unless you do some ajax stuff (if you want it to work it live). You need to send your datas behind the scene , stock them , and when the ajax response returns reload the iframe with new datas. You need to store them serverside somehow. Commented Dec 22, 2012 at 23:11
  • Take a look at how sites like jsfiddle.net and jsbin.com do it. Commented Dec 22, 2012 at 23:29

1 Answer 1

5

From your code I see no input field with name js, hence I assume the following:

echo $_POST['js'];

should be:

echo $_POST['javascript'];

Otherwise, your code should work fine.

Sign up to request clarification or add additional context in comments.

1 Comment

I think @vision is right. I tested your code and it works with this fix. Put an "alert('hello world')" in the JS code and you'll see that it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.