Solved

PHP/Javascript Apostrophe Problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XSLT 5 42
 shows up in Outlook, not OWA or on phone 3 63
Grunt No Clean Targets 6 109
Problem to page 4 74
When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

895 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

13 Experts available now in Live!

Get 1:1 Help Now