admin管理员组文章数量:1401444
I want to be able to edit something inside of a heredoc syntax. Something like this:
index.php:
$var = <<<HTML
<form action="index.php" method="get" id="forma">
<input type="radio" name="choice" value="value">Message<br>
</form>
HTML;
...
$form = $var;
js:
<script>
document.getElementById('forma').open();
document.getElementById('forma').write('<input type="submit">');
document.getElementById('forma').close();
</script>
EDIT: My goal is to have a button to go to a new page, but the button won't be present until you click OK on a JS confirm()
popup.
I want to be able to edit something inside of a heredoc syntax. Something like this:
index.php:
$var = <<<HTML
<form action="index.php" method="get" id="forma">
<input type="radio" name="choice" value="value">Message<br>
</form>
HTML;
...
$form = $var;
js:
<script>
document.getElementById('forma').open();
document.getElementById('forma').write('<input type="submit">');
document.getElementById('forma').close();
</script>
EDIT: My goal is to have a button to go to a new page, but the button won't be present until you click OK on a JS confirm()
popup.
- 1 You want to replace everything inside the form, or add to it? – tymeJV Commented Oct 16, 2013 at 14:46
-
1
document.getElementById('forma').innerHTML = '<input type="submit">';
to overwrite ordocument.getElementById('forma').innerHTML += '<input type="submit">';
to append. – gen_Eric Commented Oct 16, 2013 at 14:52 - @RocketHazmat that didn't work – Jordan Commented Oct 16, 2013 at 15:14
- Instead of plaining that solutions offered by others don't work, why don't you give us more details on what exactly you are trying to achieve and in what ways? Several valid solutions have been offered for the problem you described. If it isn't the problem you're trying to solve, consider explaining it in more detail. – lethal-guitar Commented Oct 16, 2013 at 20:47
2 Answers
Reset to default 3What you are trying to do (change the HEREDOC) is impossible.
The PHP gets interpreted on the server and the result is a HTML file with some embedded JS. Only after this HTML file gets to the client and is interpreted, the JS is executed. At this point the original PHP file containing the HEREDOC is long gone.
What you can do however is manipulate the DOM on the client side, but you should look to element.innerHTML
as an alternative to document.write
.
If I understand correctly, you'd like to have a button which bees visible after some user action. I'd suggest creating the button hidden and then displaying it via JS, instead of creating it via JS.
So your HTML bees:
$var = <<<HTML
<form action="index.php" method="get" id="forma">
<input type="radio" name="choice" value="value">Message<br>
<!-- Note the *style* attribute -->
<input type="submit" style="display: none;" id="submitBtn">
</form>
HTML;
And then in your JS, you make the button visible as soon as your confirm()
call succeeds:
function someJsHandler() {
if (confirm("Your message here")) {
var button = document.getElementById('submitBtn');
button.style.display = 'block'; // Makes the button visible
}
}
Edit: JsFiddle for this: http://jsfiddle/bQ6Un/
本文标签: javascriptCan I use documentgetElementByIdwrite() to write inside of heredocStack Overflow
版权声明:本文标题:javascript - Can I use document.getElementById.write(); to write inside of heredoc? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744207746a2595270.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论