-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%");
}
6
  • 3
    Have you tried this one: onclick='handleClick("<?php echo $phpVar ?>"); Commented Jan 18, 2015 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. Commented Jan 18, 2015 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? Commented Jan 18, 2015 at 20:42
  • @absfrm You don't use <?php inside echo statements. Commented Jan 19, 2015 at 9:37
  • @Barmar Yes , I mean he should use variable correctly. Commented Jan 19, 2015 at 16:35

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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 "<input onclick='handleClick(\"". $price . "\");' />"
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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.