1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to return string value with quotes (single or double)

Discussion in 'JavaScript' started by tllcll, Nov 9, 2005.

  1. #1
    Hi, how can I return String value retrieved from the database that consist of single or double quotes to the textbox in the form

    If the data is without quotes there isn't any
     
    tllcll, Nov 9, 2005 IP
  2. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Maybe you can use string replace function.

    In php this would be
    $variable = str_replace("x", "y", $variable);
    PHP:
    Not sure if this works with single or double quotes though - it changes character x to character y in the string $variable
     
    dave487, Nov 9, 2005 IP
  3. BurgerKing

    BurgerKing Active Member

    Messages:
    397
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Hi,

    I wouldn't have thought you would have a problem.

    Here is a short code snippet showing how to put a string with quotes into a text box.

    <html>
    <input  id="txtMyVal"  value="This is the start value" style='width:200px'>
    
    <script>
    var sTest="'this is a test'";
    
    document.getElementById("txtMyVal").value = sTest;
    </script>
    </html>
    Code (markup):
    Do you have an example which shows your problem?
     
    BurgerKing, Nov 20, 2005 IP
  4. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If your server-side code (PHP, ASP, etc) generates JS code, you need to escape quotes and slashes, so that the resulting string would look like this:

    var str = "double quote: \"; single quote: \'; slash: \/;";

    You can use any string replace function for this. If your server-side code generates HTML (e.g. a form element), you need HTML-encode double quotes, single quotes and the <> characters. For example the result will be:

    <input name="i1" value="double quote: &quot;; single quote: &#x27;; less than: &lt;; greater than: &gt;;">

    If you use PHP, use htmlspecialhars, if ASP, use Server.HTMLEncode to encode.

    J.D.
     
    J.D., Nov 25, 2005 IP