Solved

PHP/Javascript Apostrophe Problem

Posted on 2006-11-14
5
961 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 500 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

Technology Partners: 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

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

749 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