-2

I'd like to pass a PHP variable to a JS function as in:

onclick='handleClick($phpVar);'

But unfortunately, the function doesn't take the value, even after converting the php variable into a string.

The two code blocks look like this:

PHP:

while($stmt->fetch()) { 
  echo "<tr><td>" 
  . htmlspecialchars($company) 
  . "</td><td id='$price'>" 
  . htmlspecialchars($volume)
  . "</td><td id='$price'>" 
  . htmlspecialchars($price)
  . "</td><td>" 
  <button type='submit' class='submit-btn'>Submit</button>
  </td><td>
     <label tabindex='0' class='checkbox'>
     <input type='checkbox' style='opacity:0' checked='checked' onclick='handleClick($price);'/>
     <i class='close-btn fa fa-edit'></i>
     </label>
  </td></tr>";
} 

JavaScript:

function handleClick(id) {
   document.getElementById(id).style.fontSize=="150%");
}
saeppi
  • 259
  • 4
  • 16
  • 3
    Have you tried this one: `onclick='handleClick("");` – absfrm Jan 18 '15 at 20:40
  • Since it's a string, you need to quote the value before sending it to the javascript function, like absfrm suggests above. – HaukurHaf Jan 18 '15 at 20:42
  • Your PHP doesn't appear to compile. You don't appear to have assigned values to the variables you are using. You don't appear to be looking at the JavaScript you are debugging in order to find out what it actually looks like. You don't appear to be looking at your browser's JavaScript console to see if there are any error messages. What does "doesn't take the value" mean anyway? – Quentin Jan 18 '15 at 20:42
  • @absfrm You don't use ` – Barmar Jan 19 '15 at 09:37
  • @Barmar Yes , I mean he should use variable correctly. – absfrm Jan 19 '15 at 16:35
  • Possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Christoph May 16 '17 at 14:06

2 Answers2

1

You should be able to add a PHP value to that function. I tried messing around with it. I removed a quote after the last table entry and made some changes to the function call in the javascript.

while($stmt->fetch()) { 
  echo "<tr><td>" 
  . htmlspecialchars($company) 
  . "</td><td id='$price'>" 
  . htmlspecialchars($volume)
  . "</td><td id='$price'>" 
  . htmlspecialchars($price)
  . "</td><td> 
  <button type='submit' class='submit-btn'>Submit</button>
  </td><td>
     <label tabindex='0' class='checkbox'>
     <input type='checkbox' style='opacity:0' checked='checked' onclick='handleClick(".$price.");'/>
     <i class='close-btn fa fa-edit'></i>
     </label>
  </td></tr>";
} 

I hope this helps!

Liam

Liam Bolling
  • 134
  • 1
  • 8
  • 2
    If a string is supposed to be passed into the JS handleClick() function, it should be wrapped in either single or double quotes. Since double quotes are being used to print out the html and single quotes are being used for the values of the attributes, you'll probably have to used escaped double quotes around $price variable: `echo ""` – The Unknown Dev Jan 18 '15 at 21:02
1

Since you are already using htmlspecialchars on the $price variable to display it, I can only assume that you do not trust the contents of this variable. For security reasons you should also json_encode the price, then escape the whole attribute when putting javascript in to an onclick attribute. As per How to escape string from PHP for javascript?

<?php
while($stmt->fetch()) { 
  ?>  
  <tr>
    <td><?php echo htmlspecialchars($company); ?></td>
    <td id='<?php echo htmlspecialchars($price); ?>'><?php echo htmlspecialchars($volume); ?></td>
    <td id='<?php echo htmlspecialchars($price); ?>'><?php echo htmlspecialchars($price); ?></td>
    <td>
      <button type='submit' class='submit-btn'>Submit</button>
    </td>
    <td>
      <label tabindex='0' class='checkbox'>
        <input type='checkbox' style='opacity:0' checked='checked' onclick='<?php echo htmlspecialchars('handleClick(' . json_encode($price) . ');') ?>'/>
        <i class='close-btn fa fa-edit'></i>
      </label>
    </td>
  </tr><?php
}
Community
  • 1
  • 1
Phil
  • 1,996
  • 1
  • 19
  • 26