Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP/Javascript Apostrophe Problem

Posted on 2006-11-14
5
Medium Priority
?
965 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 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