-2

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.

Jordan
  • 21
  • 1
  • 2
  • 8
  • 1
    You want to replace everything inside the form, or add to it? – tymeJV Oct 16 '13 at 14:46
  • 1
    `document.getElementById('forma').innerHTML = '';` to overwrite or `document.getElementById('forma').innerHTML += '';` to append. – gen_Eric Oct 16 '13 at 14:52
  • @RocketHazmat that didn't work – Jordan Oct 16 '13 at 15:14
  • Instead of complaining 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 Oct 16 '13 at 20:47

2 Answers2

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.

Tibos
  • 27,507
  • 4
  • 50
  • 64
  • Then would there be another way to put html inside a php variable, and then add to the html via javascript? – Jordan Oct 16 '13 at 15:15
  • 1
    Perhaps if you explain a bit more about your context and why you think you need to alter the php variable via JS code we would be able to help more. – Tibos Oct 16 '13 at 15:30
  • 1
    You don't need to change the PHP variable, it is enough for your JS to manipulate the DOM as mentioned in more detail in other answers. – Tibos Oct 16 '13 at 16:11
  • But I need to put html into the php variable. Is there a way to replace the html server-side? – Jordan Oct 16 '13 at 17:51
  • @Jordan You seem to be a bit unclear about the distinction between client-side and server-side code. The only way to change the server-side HTML would be to load it from a database, read it from a file, alter it depending on parameters etc. There is neither a way for a JS script to change a server-side PHP file, nor is this necessary. – lethal-guitar Oct 16 '13 at 20:45
0

If I understand correctly, you'd like to have a button which becomes 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 becomes:

$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.net/bQ6Un/

lethal-guitar
  • 4,438
  • 1
  • 20
  • 40
  • You forgot to end the quotation: `document.getElementById('submitBtn');` and it doesn't make the button visible – Jordan Oct 16 '13 at 16:32
  • I've added the missing quote. And it works for me in Firefox. See http://jsfiddle.net/bQ6Un/. You have to make sure the JS function is somehow called, of course. In my example, I used a link, but you're free to invoke it via other means. You didn't specify in your question how you intend to invoke the `confirm`-dialog. – lethal-guitar Oct 16 '13 at 20:38
  • It works when there is no HEREDOC. The JSFiddle doesn't show the php. Otherwise, it works. Thanks, though. – Jordan Oct 17 '13 at 01:11
  • Well, JsFiddle is only for client-side code, so it cannot interpret PHP - only plain HTML and JavaScript. – lethal-guitar Oct 17 '13 at 12:21