Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call a javascript function in a php web page?
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php">How to call a JavaScript function from PHP?</a> </p> </blockquote> <p>I have a php page in which I echo certain things depending on the user privilege (admin or user). I would like to know if I can call a javascript function when a button is clicked.</p> <pre><code>&lt;?php if ($_SESSION['access_rights']=='admin') { $id = $_GET['id']; $userName = $_GET['username']; echo "&lt;a href='../view/confirmDelete.php?id=$id&amp;type=task&amp;username=$userName'&gt;&lt;input type='button' class='delete-btn' value=''/&gt;&lt;/a&gt; &lt;a href='../view/editTask.php?id=$id&amp;username=$userName'&gt;&lt;input type='submit' class='edit-btn' value=''/&gt;&lt;/a&gt;"; } ?&gt; </code></pre> <p>The function I would like to call is as follows. This is placed in the head of the page.</p> <pre><code>&lt;script type="text/javascript"&gt; function deleteTask(id,task,name) { var conBox = confirm("Are you sure you wanna delete task assigned to "+ name + " ?"); if (conBox) { location.href="&lt;?=$_SERVER['PHP_SELF'];?&gt;" } else { return; } } &lt;/script&gt; </code></pre> <p>I was wondering if the function 'deleteTask()' can be called via a href like </p> <pre><code>&lt;a href="javascript" deleteTask('&lt;?=id;?&gt;','task','&lt;?=userName?&gt;');"&gt;Delete me&lt;/a&gt; </code></pre> <p>else using onclick="deleteTask();</p> <p>I am required to pass arguments to the function. I am unable to the javascript call in href as it requires quote. Any ideas?</p>
 

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