passing multiple php variables into javascript function [duplicate]
I'm trying to pass multiple values from PHP into my javascript function. The function is simple right now, just trying to show a popup with the values:
<script type="text/javascript">
function showMapsInfo(name, ctr) {
开发者_如何转开发 alert('Info = '+name +' '+ctr);
}//function showMapsInfo
</script>
When I just pass in one value, name or ctr, it works fine. However when passing in two or more values no alert occurs. Is there a reason this isn't working? I'm guessing if this is impossible I'll have to use AJAX to get the job done?
Here is the relevant PHP code. I am making multiple forms on the page, every id is unique via a ctr. I read in the $maps_name from a database. This I can output to the screen fine, so no issue there.
echo('<button type="button" id="button'.$ctr.'" onClick="showMapsInfo('.$maps_name.', '.$ctr.');"><img src="img/maps_logo.gif"></button><br/>');
I'm guessing you just need to quote $maps_name
and $ctr
beforehand (since they're most likely strings:
echo('<button type="button" id="button'.$ctr.'" onClick="showMapsInfo(\''.str_replace("'", "\\'", $maps_name).'\', \''.str_replace("'", "\\'", $ctr).'\');"><img src="img/maps_logo.gif"></button><br/>');
精彩评论