venkywonka
venkywonka

Reputation: 145

Why is the following onclick function not executing?

I would like to pass four arguments event, $row['title'], $row['username'] and $row['date'].

<?php
echo "<script>
  function increasevotes(e,location,user,date)
  {
    e.preventDefault();
    console.log(\"Hi!\");
  }
  </script>";

//I've proper code here for fetching the mySQL query results into $rows and that part is working perfectly.

while($rows=mysqli_fetch_array($result))
{
if($rows['public']=="yes")
{echo "<span class=\"right\" id=\"nolikes\">{$rows['vote']}</span><a href=\"\" onclick=\"increasevotes(event,\"{$rows['title']}\",\"{$rows['username']}\",\"{$rows['date']}\")\"><img src=\"img/like.png\" class=\"right\" width=\"30\" height=\"30\"/></a>";
}
?>

What is actually being done, is the page is being refreshed instead. When I click view source page, this is shown:

<span class="right" id="nolikes">0</span>
<a href="" onclick="increasevotes(event,"chennai","venkat","01/07/2017")"><img src="img/like.png" class="right" width="30" height="30"/>
</a>

Upvotes: 0

Views: 45

Answers (1)

brk
brk

Reputation: 50291

Change double quotes of function parameter to single quotes

onclick="increasevotes(event,'chennai','venkat','01/07/2017')"

or use \ to escape the quotes.

Upvotes: 1

Related Questions