?
Solved

PHP/Javascript Apostrophe Problem

Posted on 2006-11-14
5
Medium Priority
?
964 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17943025
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
ID: 17943028
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
ID: 17945023
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 2000 total points
ID: 17945308
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
ID: 17947116
The last one was the only answer that actually worked. The others were still giving the same error..

Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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…
Suggested Courses

765 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