Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming your real question is "how can I take data in PHP and make a copy of that data available to Javascript".</p> <p>There are two main approaches you could take:</p> <ol> <li>Have your Javascript, when triggered, attempt to get information from the server via AJAX call. That would be a separate PHP script and the JS would have to pass in some sort of ID or other information to help you figure out what it needs. <ul> <li>Requires a separate PHP script and extra plumbing.</li> <li>Better when the amount of data is large</li> <li>Better when there are a lot of these buttons and users won't use that many of them</li> <li>Forms the foundation of much fancier stuff later on</li> </ul></li> <li>Embed the information into the HTML, and have the Javascript read it out. <ul> <li>Easier to do</li> <li>Page is bigger</li> <li>May become a problem once you start doing it a lot</li> <li>You'll probably need to do this just a little bit even after AJAX gets used, to pass IDs and things.</li> </ul></li> </ol> <p>I'd suggest you try the "quick and dirty" way for the moment, just to get a feel for it. For example, in your PHP page:</p> <pre><code>&lt;?php /* ... */ $defaultMessage = "Hello world! Watch me contain single quotes (') and double quotes(\") and other things!"; /* ... */ ?&gt; &lt;button id="new_message" onclick="createMessage(&lt;?= htmlentities(json_encode($defaultMessage))?&gt;"&gt;New Message&lt;/button&gt; </code></pre> <p>I'm not 100% sure on the escaping, but test with single and double quotes in the data to ensure nothing goes awry, and remember that you need to escape it one way to avoid breaking HTML, and again in another way to avoid breaking javascript. Then in your JS file:</p> <pre><code>function createMessage(defaultMessage) { // ... textarea.value = defaultMessage; // ... } </code></pre> <p>In this way you've put data from PHP into the HTML output, and then later (after the PHP script has stopped running) the user's browser gets the HTML and can can pull that data back out for whatever purpose it needs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload