0

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/>');
hakre
  • 193,403
  • 52
  • 435
  • 836
user387049
  • 6,647
  • 8
  • 53
  • 55

1 Answers1

5

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/>');
netcoder
  • 66,435
  • 19
  • 125
  • 142