Passing a String from PHP to a JavaScript function

Solution:1

you need to put quotes around the string. try this:


echo("<td onclick = 'print_(\'".$file[$i]->Name."\');' >". $files[$i]->Name."</td>");

also make sure your $file[$i]->Name doesn’t contain quotes

Solution:2

You could try something like this:

<?php
...
$scriptCode="";
...(looping) 

echo("<td id='print_td$i' >". $files[$i]->Name."</td>");
$scriptCode .= '$("#print_td'.$i.'").click(function(){
  var id = '.$i.';
  //Do whatever JS you need to here, possibly: print_(id);
});';

... (end loop)

?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
  <?php echo $scriptCode; ?>
});
</script>

This is a little more extensive, but it will get you more flexibility in the future.