Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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/>');
share|improve this question

1 Answer

up vote 2 down vote accepted

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/>');
share|improve this answer
Thanks for the help, would've caught that one! – user387049 Nov 5 '10 at 16:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.