Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To print a variable into Javascript you just add curly braces (this only works when you are emitting a <em>double quoted</em> string, so:</p> <pre><code> echo "&lt;td&gt;&lt;a href='#' onclick='disp_confirm({$row-&gt;university})'&gt;Delete&lt;/a&gt;&lt;/td&gt;"; </code></pre> <p>will work and</p> <pre><code> echo '&lt;td&gt;&lt;a href="#" onclick="disp_confirm({$row-&gt;university})"&gt;Delete&lt;/a&gt;&lt;/td&gt;'; </code></pre> <p>will <em>not</em> work. The same for <code>{$variables}</code>, <code>{$array['of values']}</code> etc.</p> <p>(In the above code, <code>$row-&gt;university</code> has to be an integer, or you'll get a Javascript error. If so, quote the value with escaped single quote marks)</p> <p><strong>UPDATE</strong> (thanks to <a href="https://stackoverflow.com/users/204701/mihai-iorga">Mihai Iorga</a>): you must take into account how the variable will interact with the Javascript code. It is not enough to quote strings, because the variable might itself contain a quote mark. So if your university was "Shi'Kahr University", the HTML would become</p> <pre><code>onclick='disp_confirm(\'Shi'Kahr University\')' </code></pre> <p>and the Javascript would be immediately broken. So you also have to use <code>addslashes()</code> to ensure that your string is properly quoted. Since you are writing a string <em>inside a string</em>, you need to call <code>addslashes()</code> twice, to get</p> <pre><code>onclick='disp_confirm(\'Shi\\\'Kahr University\')' </code></pre> <p>To pass a Javascript value to PHP script on server side, you can use AJAX (e.g. jQuery library) or you can add the value to the URL and reload the page:</p> <pre><code> alert("Submitting " + value); location.href = '?variable=' + value; </code></pre> <p>The script will <strong>execute again from the beginning</strong> (which may or may not be what you want), and <code>$_GET['variable']</code> will then contain <em>value</em>. You can distinguish the two cases with an <code>if</code>:</p> <pre><code>&lt;?php if (isset($_GET['variable'])) { // This is the second round } else { // This is the first time } // common code goes here ?&gt; </code></pre> <p>Finally, there are libraries that allow mixing PHP and Javascript. They basically work as above, but they take care of the heavy lifting, variable passing, escaping, and calling back. See for example <a href="http://www.xajaxproject.org/" rel="nofollow noreferrer">XAJAX</a>, but several others exist.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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