Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript functions in PHP echo statement [closed]

I am trying to use javascript functions in a php echo statement. The showme and onclick function are being used and values must be passed in the showme function.

The code below is not working.

 echo"
  <ul>
  <li><a href='javascript:void(0);' onclick='showme('Test','','10.70725','-61.55391','','Tester');' >$id</a></li>
</ul>
  ";

How do I fix this?

like image 764
user1704514 Avatar asked Jan 31 '26 16:01

user1704514


2 Answers

echo"
  <ul>
  <li><a href='javascript:void(0);' onclick=\"showme('Test','','10.70725','-61.55391','','Tester');\" >$id</a></li>
</ul>
  ";
like image 165
Mohit Bumb Avatar answered Feb 02 '26 04:02

Mohit Bumb


So don't echo so much using PHP simply make it like this, and also add the " quotes around your showme()

<ul>
  <li>
    <a href="javascript:void(0);" onclick="showme('Test','','10.70725','-61.55391','','Tester');"><?php echo $id; ?></a>
  </li>
</ul>

Edit: As you said you are running this in a while loop you can do it like this

I assume that you are extracting the data from the database

<?php
  while($blah = mysqli_fetch_array($fetch_blah)) {
?>
<!--HTML Part Goes Here-->
<ul>
  <li>
    <a href="javascript:void(0);" onclick="showme('Test','','10.70725','-61.55391','','Tester');"><?php echo $blah['id']; ?></a>
  </li>
</ul>
<?php
}
?>
like image 36
Mr. Alien Avatar answered Feb 02 '26 04:02

Mr. Alien