Solved

PHP/Javascript Apostrophe Problem

Posted on 2006-11-14
5
958 Views
Last Modified: 2012-05-05
I'm running a store locator script in PHP that incorporates Google Maps and am having trouble displaying an apostrophe for store names and addresses. A value being passed to the Google API has to be pulled from a database using PHP. Unfortunately the javascript pukes when it runs into an apostrophe. Here is the script:

<script type="text/javascript">

var map = new GMap(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.centerAndZoom(new GPoint(<?php echo $lon ?>, <?php echo $lat ?>), 7);

function createMarker(point, number)
{
var marker = new GMarker(point);

var html = number;
GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
return marker;
};

<?php

while (!$rs2g->EOF)
{
$lat2g = $rs2g->Fields['latitude']->Value;
$lon2g = $rs2g->Fields['longitude']->Value;
$nameg = $rs2g->Fields['resname']->Value;
$add1g = $rs2g->Fields['resadd1']->Value;
$add2g = $rs2g->Fields['resadd2']->Value;
$add4g = $rs2g->Fields['resadd4']->Value;
$phoneg = $rs2g->Fields['resphone']->Value;
$flagg = substr($rs2g->Fields['reszip']->Value,0,5);
$webg = $rs2g->Fields['resweb']->Value;

echo "var point = new GPoint(" . $lon2g . "," . $lat2g . ");\n";
echo "var marker = createMarker(point, '<div id=\"infowindow\" style=\"white-space: nowrap;\">" . $nameg . "</div>');\n";
echo "map.addOverlay(marker);\n";
echo "\n";


 
      $rs2g->MoveNext();
}
?>

</script>

The problem is specifically centered in the third to last echo line where $nameg is being called. As you can see by the script, apostrophes are used to define a vlaue for createMarker. Some of the locations have an apostrophe in their name and it's creating the error by prematurely ending the value for createMarker. Any workarounds? And while you're at it, can I modify that info window to also display addresses and phone numbers with line breaks?

Thanks!
0
Comment
Question by:cbeaudry1
5 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
Comment Utility
Anything you're writing out to HTML (ESPECIALLY inside Javascript, but really anywhere) should use htmlentities().

Replace $nameg with htmlentities($nameg)
0
 
LVL 19

Expert Comment

by:VoteyDisciple
Comment Utility
Oh, and... sure, you can display anything you want in there.  Use an ordinary <br /> tag to break to a new line, just as you would in any other chunk of HTML.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
alternatively use

echo "var marker = createMarker(point, '<div id=\"infowindow\" style=\"white-space: nowrap;\">'+escape('" . $nameg . "')+'</div>');\n";
0
 
LVL 6

Accepted Solution

by:
aescnt earned 500 total points
Comment Utility
actually, use addslashes(htmlentities($nameg)) :)

addslashes() will escape the slashes properly (necessary for writing JS strings with PHP), and htmlentities() will escape the < > and other relevant HTML symbols (necessary for printing HTML).
0
 

Author Comment

by:cbeaudry1
Comment Utility
The last one was the only answer that actually worked. The others were still giving the same error..

Thanks!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now