Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript new Option fn with an apostrophe - causing grief 2

Status
Not open for further replies.

LTeeple

Programmer
Aug 21, 2002
362
CA
Hi all,
I'm working on a javascript function using PHP to populate a dropdown list from a database. I'm sure that my problem is actually in my javascript syntax, so I am posting in this forum.

The key values I'm talking about appear in my database as follows:
Code:
+----+-------------------+
| ID | PLACE_NAME        |
+----+-------------------+
|  9 | Laura\'s Place    |
+----+-------------------+
When I attempt to add these values to my option list by doing the following:
Code:
$sql = "SELECT * FROM PLACE WHERE CITY_ID = $city";
$results = mysql_query($sql,$connection)
  or die("SQL Error on query: " . $sql . "<br>" . mysql_error());
while ($row = mysql_fetch_array($results)){
  $place_id = $row['ID'];
  $place = stripslashes($row['PLACE_NAME']);
  $display_block .= "document.forms['places_form'].elements['places'].options[$ctr] = new Option('$place',$place_id);\n";
  $ctr++;
}
return $display_block;
// returns $display_block to function that writes the javascript

When I go to run the page, I can see the javascript error:
Code:
Error: missing ) after argument list
Source Code: document.forms['places_form'].elements['places'].options[1] = new Option('Laura's Place',9);  
//the error pointer is pointing at the 's' following the apostrophe

Obviously I should be escaping the apostrophe. The problem is, escaping an apostrophe looks pretty lousy in a production form.

Is there any other way of writing the new Option () function?

Thanks in advance.

[cheers]
Cheers!
Laura
 
Code:
 document.forms['places_form'].elements['places'].options[1] = new Option([COLOR=red]"[/color]Laura's Place[COLOR=red]"[/color],9);

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
[banghead]
 
Why does it look lousy in a production form to escape characters? They don't actually show on the page. In the 5+ years I've been using JS, this is the first time I've heard that one.

You can use PHP's addcslashes() to take care of this for you, or you can use double quotes instead of single quotes:

$display_block .= "document.forms['places_form'].elements['places'].options[$ctr] = new Option(\"$place\",$place_id);\n";

or

$display_block .= "document.forms['places_form'].elements['places'].options[$ctr] = new Option('" . addcslashes($place) . "',$place_id);\n";

Lee

 
kaht

OMG that was so obvious. I think I need to change my signature icon to: [banghead]

I have another question, starting a new thread. Perhaps if you have a moment, you could sneak a peek for me.

TY so much!

[cheers]
Cheers!
Laura
 
Lee had the right answer too, and provided a bit more insight. Perhaps you should give him some
star.gif
love too [wink]

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
[banghead]
 
trollacious,

(quote)Why does it look lousy in a production form to escape characters?(/quote)

I meant the escape character actually showing in the dropdown list:

i.e. Laura/'s Place literally appearing as an option, as opposed to the much preferred Laura's Place.

Perhaps I didn't word my thoughts properly. Regardless, kaht answered my question perfectly. Sometimes the answer is right in front of you!

[cheers]
Cheers!
Laura
 
The forward slash (/) is NOT an escape character. You have to use the backward slash: \

Lee
 
No problem!
headbang.gif
[rockband]
headbang.gif


-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top