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.

Share Improve this question edited Oct 16, 2013 at 15:40 Jordan asked Oct 16, 2013 at 14:44 JordanJordan 311 gold badge2 silver badges8 bronze badges 4
  • 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 or document.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
Add a ment  | 

2 Answers 2

Reset to default 3

What 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