通过PHP POST传递JavaScript代码

通过PHP POST传递JavaScript代码,javascript,php,html,post,Javascript,Php,Html,Post,我有两个文本区域,一个iframe和一个submit按钮。它们都在同一页上。我想做的是,;用户将在第一个文本区域中编写一些html和javascript代码。第二个已隐藏以传递javascript代码。用户单击“提交”按钮后,将在iframe中看到结果。实际上,我所说的有点像w3school的尝试页面 我的提交按钮是: <INPUT type="submit" value="Run the Codes" name="submit" onclick="sendthem()">

我有两个文本区域,一个iframe和一个submit按钮。它们都在同一页上。我想做的是,;用户将在第一个文本区域中编写一些html和javascript代码。第二个已隐藏以传递javascript代码。用户单击“提交”按钮后,将在iframe中看到结果。实际上,我所说的有点像w3school的尝试页面

我的提交按钮是:

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

我的文本区是:

<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. 
//用户的文本区域
//这实际上是隐藏的(style=“display:none;”),但为了查看发生了什么,我临时将其显示出来。
我的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
}
函数sendthem() { var t=document.getElementById(“textareaCode1”).value;//可见的textarea t=t.replace(//g,“更大”);//通过php post传递js代码的字符串操作 t=t.replace(//=/g,“EQUAL”);//通过php post传递js代码的字符串操作 document.getElementById(“textareaCode”).value=t;//被操纵的代码进入隐藏的textarea document.getElementById(“sampleform”).submit();//发送表单 } 我的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.
?>

这是一个截图(我希望它有帮助)

现在。。我认为我的问题在于PHP部分。有人能告诉我,为什么在撤销更改后,我看不到代码的JS部分吗


请注意,我不想用jQuery或AJAX等更改所有代码。。我想修复这个代码。提前感谢…

您不需要执行所有这些字符串替换。当您发布表单时,它将被正确的URL编码,PHP将自动对其进行解码。@Barmar它不会。我要处理这个问题4天。PHP POST无法以任何方式获取JS代码。您需要将JS代码放入iframe中的
块中。@Barmar但是如何?不仅仅是JS代码,在
之间还存在
onclick=StartWorker()>
部分。。通过后,它变成
onclick>
=StartWorker()
部分刚刚丢失。我想不出
onclick
丢失的原因。控制台中是否有任何错误?