【发布时间】:2014-06-17 14:55:35
【问题描述】:
我正在做一个特殊的项目,我几乎完成了。我的问题是*sending parameter to iframe*。
我的目标是创建 simple 编辑器,例如 codepen 或 jsfiddle。
这些是我的 ajax 代码:
$.ajax({
data: {html: html, css: css, js: js},
success: function(r) {
if(r) {
$("#preview").html(r.preview);
} else {
alert("error");
}
}
});
我的 ajax.php
$html = $_POST["html"];
$css = $_POST["css"];
$js = $_POST["js"];
$r = array();
function d($html, $css, $js) {
$iframe .= '<iframe src="iframe.php">';
$iframe .= '</iframe>';
return $iframe;
}
$r["preview"] = d($html, $css, $js);
echo json_encode($r);
最后是我的 iframe.php
function prev($html = "", $css = "", $js = "") {
$iframe .= '<html>';
$iframe .= '<head>';
$iframe .= '<style>' . $css . '</style>';
$iframe .= '<script>' . $js . '</script>';
$iframe .= '<script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>';
$iframe .= '</head>';
$iframe .= '<body>';
$iframe .= $html;
$iframe .= '</body>';
$iframe .= '</html>';
return $iframe;
}
echo prev(); // which parameters I use?
它正在工作,但我没有发送来自 textarea 的参数?我没有关联 ajax.php 和 iframe.php。
【问题讨论】:
-
不能把页面上已经固定的
iframe设置好,然后把值直接post到iframe.php中,而不是通过另一个页面传递吗? -
$iframe .= '<iframe src="iframe.php?html='.$html.'&css='.$css.'&js='.$js.'">';我当时这样用"没用。但我相信我可以用 POST 方法解决。 -
我的意思是,你为什么要在 ajax 响应中创建
<iframe>?为什么不保留已经创建的 iframe,然后将带有目标的表单发布到 iframe? -
我分析了codepen、jsfiddle等。使用 iframe 是最好的解决方案。其他方式是错误的和不充分的。有一个地方我错过了。
-
我不是告诉你不要使用
iframe...我只是说你应该已经在页面上设置了<iframe>!! JsFiddle 本身已经在页面上设置了所有四个 iframe...它不会动态创建它!!!
标签: javascript php jquery css ajax