String in php and js script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • php_mysql_beginer911

    #1

    String in php and js script

    Hi everyone ..
    i am trying to use a string which was saved by php form and contain
    single quote ' or double quote

    " .. when i using that string in my js script function like
    function

    onclick='js_tes t("<?=$string;? >")'

    if $string contains double quote it will simply break the function ..
    and same goes for single quote if i

    surround $string with single quote i tried using escape and unescape
    but that means .. every place i am using php ddata which might contain
    single or double quote i have to use those javascript

    function . as i am very new to programming if somebody could advise
    what is the best way to deal

    with string when we use js script and php

    hope you can understand my english and hope somebody will reply soon

    thanks
  • steve.high@gmail.com

    #2
    Re: String in php and js script

    It's generally considered best practices to wrap HTML elements in
    double quotes.
    So, your onclick event handler should look like this:

    onclick="js_tes t('<?php echo $string; ?>');"

    (Note that it is also considered good form to use <?php ... ?to
    denote PHP code at all times.)

    Depending on the contents of $string, you might also want to use the
    addslashes() function, like so...
    onclick="js_tes t('<?php echo addslashes($str ing); ?>');"

    Hope this helps.

    On Dec 8, 9:51 pm, php_mysql_begin er911 <deepa...@gmail .comwrote:
    Hi everyone ..
    i am trying to use a string which was saved by php form and contain
    single quote ' or double quote
    >
    " .. when i using that string in my js script function like
    function
    >
    onclick='js_tes t("<?=$string;? >")'
    >
    if $string contains double quote it will simply break the function ..
    and same goes for single quote if i
    >
    surround $string with single quote i tried using escape and unescape
    but that means .. every place i am using php ddata which might contain
    single or double quote i have to use those javascript
    >
    function . as i am very new to programming if somebody could advise
    what is the best way to deal
    >
    with string when we use js script and php
    >
    hope you can understand my english and hope somebody will reply soon
    >
    thanks

    Comment

    • Michael Fesser

      #3
      Re: String in php and js script

      ..oO(steve.high @gmail.com)
      >It's generally considered best practices to wrap HTML elements in
      >double quotes.
      Attributes. And both single and double quotes are perfectly valid. In
      this case it might make a difference because of the JS, but usually it
      doesn't matter.

      Micha

      Comment

      • allain

        #4
        Re: String in php and js script

        On Dec 9, 6:11 am, Michael Fesser <neti...@gmx.de wrote:
        .oO(steve.h...@ gmail.com)
        >
        It's generally considered best practices to wrap HTML elements in
        double quotes.
        >
        Attributes. And both single and double quotes are perfectly valid. In
        this case it might make a difference because of the JS, but usually it
        doesn't matter.
        >
        Micha
        XHTML allows you to use both single or double, but it's best to use
        the double quotes since it's what most people expect.

        Also, addslashes is perfect for this problem. Sounds like you'll need
        to sanitize the string with htmlspecialchar s.

        My 2 cents,
        Allain

        Comment

        • Michael Fesser

          #5
          Re: String in php and js script

          ..oO(allain)
          >XHTML allows you to use both single or double, but it's best to use
          >the double quotes since it's what most people expect.
          Who expects that?

          Micha

          Comment

          • Rik Wasmus

            #6
            Re: String in php and js script

            On Mon, 10 Dec 2007 21:25:48 +0100, Michael Fesser <netizen@gmx.de wrote:
            .oO(allain)
            >
            >XHTML allows you to use both single or double, but it's best to use
            >the double quotes since it's what most people expect.
            >
            Who expects that?
            Apparantly, some very, very bad parsers(1). And as XHTML is rendered as
            tag soup most of the time anyway, and it's perfectly legal to use either
            one, just take your pick. I just prefer double quotes because that's what
            I'm used to, no other greater good in mind then ease of code :).

            (1) http://worsethanfailure.com/Articles...ion-Error.aspx
            --
            Rik Wasmus

            Comment

            • Michael Fesser

              #7
              Re: String in php and js script

              ..oO(Rik Wasmus)
              >On Mon, 10 Dec 2007 21:25:48 +0100, Michael Fesser <netizen@gmx.de wrote:
              >.oO(allain)
              >>
              >>XHTML allows you to use both single or double, but it's best to use
              >>the double quotes since it's what most people expect.
              >>
              >Who expects that?
              >
              >Apparantly, some very, very bad parsers(1). And as XHTML is rendered as
              >tag soup most of the time anyway, and it's perfectly legal to use either
              >one, just take your pick. I just prefer double quotes because that's what
              >I'm used to, no other greater good in mind then ease of code :).
              That's perfectly OK. I also usually use double quotes, but often enough
              I use single quotes, if it makes my PHP code more readable:

              print "<img src='$someVar' alt='...'>\n";

              vs.

              print "<img src=\"$someVar\ " alt=\"...\">\n" ;

              or

              print '<img src="'.$someVar .'" alt="...">'."\n ";

              I definitely prefer the first one. Every browser accepts it and for the
              website user it doesn't matter at all.

              Micha

              Comment

              Working...