通过PHP POST传递JavaScript代码

时间:2014-03-06 06:49:16

标签: javascript php html post

我有两个textareas,一个iframe和一个提交按钮。它们都在同一页面中。我想做的是;用户将一些html和javascript代码编码到第一个textarea。第二个已被隐藏以传递javascript代码。用户点击提交按钮后,他/她将在iframe中看到结果。实际上我所说的就像w3school的尝试页面。

我的提交按钮是:

<INPUT type="submit" value="Run the Codes" name="submit" onclick="sendthem()"> 

我的textarea是:

<TEXTAREA id="textareaCode1" style="WIDTH:100%;HEIGHT:400px" name="code22" rows="21" wrap="logical" cols="42"><?=$code;?></TEXTAREA> //the user's textarea

<textarea  id="textareaCode"  style="WIDTH:100%;HEIGHT:400px" name="code"  rows="21" wrap="logical" cols="42"><?=$code;?></textarea> //this is actually hidden (style="display:none;") one but to see what happens, temporary i made it visible. 

和我的sendthem()函数是:

function sendthem()
{
var t=document.getElementById("textareaCode1").value; //the visible textarea
t=t.replace(/</g,"SMALLER"); //string manipulation to pass js codes via php post
t=t.replace(/>/g,"GREATER"); //string manipulation to pass js codes via php post
t=t.replace(/=/g,"EQUAL"); //string manipulation to pass js codes via php post
document.getElementById("textareaCode").value=t; //manipulated code goes in to hidden textarea
document.getElementById("sampleform").submit(); //send the form
}

我的PHP代码是:

<?
$code = $_POST['code'];
echo "$code"; // here i can see the manipulated code like "SMALLERhtmlGREATER"
?><br/><? // to try it :
$code = str_replace("GREATER", ">", $code); // I reverse the changes
$code = str_replace("EQUAL", "=", $code);
$code = str_replace("SMALLER", "<", $code);
printf("%s",$code); //and there is no js codes.. i tried echo or other php print commands also.
?>

这是截图http://i57.tinypic.com/sghcba.png(我希望它有帮助)

现在..我认为我的问题出在PHP部分。任何人都可以告诉我为什么在撤消更改后我无法看到JS部分的代码?

并且请,我不想用jQuery或AJAX等更改我的所有代码。我想修复此代码。提前谢谢......

0 个答案:

没有答案