user1079178
user1079178

Reputation: 133

Problems passing PHP string to javascript function

Im having problems passing PHP Strings to Javascript functions, I'v read a number of the post regarding this problem and tried several methods but none of them have worked for me. basically I have two functions one written in PHP that pulls information from a database and the other written in Javascript designed to allow me to geocode an address for google maps and pass some info to be added to the info window, the two bits of code are shown below:

PHP

try {
        $bubbleData = $dbConnection->getBubbleData();
    } catch (Exception $e) {
        echo "The following error occoured while attempting to get Google map info window data" .
        " " . $e->getMessage() .
        " " . "In file" .
        " " . $e->getLine() .
        " " . "on line" .
        " " . $e->getLine();
    }

    if (mysql_num_rows($bubbleData) == 0) {
        echo "No Placement data found to populate map";
    } else {
        while ($row = mysql_fetch_array($bubbleData)) {
            $companyName = $row['Company_Name'];
            $title = $row['Title'];
            $address_1 = $row['Address_Line_1'];
            $address_2 = $row['Address_Line_2'];
            $address_3 = $row['Address_Line_3'];
            $address_4 = $row['Address_Line_4'];
            $post_Code = $row['Post_Code'];
            $forename = $row['Forename'];
            $surname = $row['Surname'];

            $fullAddress = $address_1 . " " . $address_2 . " " . $address_3 . " " . $address_4 . " " . $post_Code;

            $partAddress_1 = $address_1 . " " . $address_2;
            $partAddress_2 = $address_3 . " " . $address_4;

            $infoText = "<h4>" . $title . "</h4>" .
                    '<b>' . "Company name:" . '</b>' . " " . $companyName .
                    "</br>" .
                    "<b>" . "Employee name:" . '</b>' . " " . $forename . " " . $surname . '</br>' .
                    "<b>" . "Company address:" . "</b>" . " " . $partAddress_1 . '</br>' .
                    $partAddress_2 . '</br>' . $post_Code;

    echo "<SCRIPT LANGUAGE='javascript'>
    geocodeAddress(<?php echo json_encode($fullAddress);?>,<?php echo json_encode($infoText);?>);
    </SCRIPT>'";
        }
    }

Javascript

function geocodeAddress (address,infoText) {

            var geocoder = new google.maps.Geocoder();
            geocoder.geocode( {'address': address}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    loadMarker(results[0].geometry.location,infoText,address);
                    latlngArray.push(results[0].geometry.location);
                } else {
                    alert("Geocode was not successful for the following reason: " +" "+  status);
                }
            });
        }re

Now I know that the two work fine individually but whenever I try to call the javascript in side the PHP and pass it the variables I ge the following error in firebug:

Invalid value for property address:

I've been trying to make this happen all day, Ive used regexs plane echos and the json_encode() function and nothings worked can anyone help?

Thanks in advance

Upvotes: 0

Views: 1789

Answers (2)

Yotam
Yotam

Reputation: 888

At least in my case, I have noticed that

<?php ...foo... ?>

segments in the HTML body, get php-evaluated only if the file extension is .php. That is, I had to rename:

mv foo.html foo.php

to have php-values passed into javascript variables.

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270609

You have nested <?php ?> inside PHP code, which will not be interpreted as PHP. Rather, it just prints into your output as a string.

 // Instead of
 echo "<SCRIPT LANGUAGE='javascript'>
    geocodeAddress(<?php echo json_encode($fullAddress);?>,<?php echo json_encode($infoText);?>);
    </SCRIPT>'";

 // Change to
 echo "<SCRIPT LANGUAGE='javascript'>
    geocodeAddress(" . json_encode($fullAddress) . "," . json_encode($infoText) . ");
    </SCRIPT>'";

Upvotes: 4

Related Questions