JS/PHP Integration

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

    JS/PHP Integration

    Say I'm using a snippet like
    $phpfoo = "a\nb\nc";
    var jsfoo = "<?php echo $phpfoo;?>";

    This errors out because JS sees the newline, and it needs to be
    escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
    retrieved and displayed OK.)

    What do you folks use in this kind of situation? Thanks, all.

    AS

  • Peter Michaux

    #2
    Re: JS/PHP Integration

    On Apr 27, 7:35 am, ashore <3ash...@comcas t.netwrote:
    Say I'm using a snippet like
    $phpfoo = "a\nb\nc";
    var jsfoo = "<?php echo $phpfoo;?>";
    >
    This errors out because JS sees the newline, and it needs to be
    escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
    retrieved and displayed OK.)
    >
    What do you folks use in this kind of situation? Thanks, all.
    If the $phpfoo string in PHP contains a new line then this is
    something you solve with PHP before you print it into the HTML page.
    Turn the new line character into somthing that will print as "\n" into
    the page if you want the JavaScript to see the new line.

    Peter

    Comment

    • Martin Honnen

      #3
      Re: JS/PHP Integration

      ashore wrote:
      Say I'm using a snippet like
      $phpfoo = "a\nb\nc";
      var jsfoo = "<?php echo $phpfoo;?>";
      >
      This errors out because JS sees the newline, and it needs to be
      escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
      retrieved and displayed OK.)
      PHP has addslashes e.g.
      var jsfoo = "<?php echo addslashes($php foo); ?>";
      that should help (although it is there in PHP to deal with escaping
      strings for data base stuff).


      --

      Martin Honnen

      Comment

      • -Lost

        #4
        Re: JS/PHP Integration

        ashore wrote:
        Say I'm using a snippet like
        $phpfoo = "a\nb\nc";
        var jsfoo = "<?php echo $phpfoo;?>";
        >
        This errors out because JS sees the newline, and it needs to be
        escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
        retrieved and displayed OK.)
        >
        What do you folks use in this kind of situation? Thanks, all.
        I imagine we would generate content that JavaScript could handle.

        <script type="text/javascript">
        var blah = '<?php print 'a\\nb\\nc'; ?>';
        </script>

        For example.

        --
        -Lost
        Remove the extra words to reply by e-mail. Don't e-mail me. I am
        kidding. No I am not.

        Comment

        • Christoph Burschka

          #5
          Re: JS/PHP Integration

          Martin Honnen schrieb:
          ashore wrote:
          >
          >Say I'm using a snippet like
          > $phpfoo = "a\nb\nc";
          > var jsfoo = "<?php echo $phpfoo;?>";
          >>
          >This errors out because JS sees the newline, and it needs to be
          >escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
          >retrieved and displayed OK.)
          >
          >
          PHP has addslashes e.g.
          var jsfoo = "<?php echo addslashes($php foo); ?>";
          that should help (although it is there in PHP to deal with escaping
          strings for data base stuff).
          >
          >
          Actually, it's not JS that turns "\n" into a newline, it's PHP.

          If single quotes (like below) don't make it go away, *then* it's time to
          look for ways to escape it on the Javascript side; not before.
          > <?php $phpfoo = 'a\nb\nc'; ?>
          > var jsfoo = "<?php echo $phpfoo;?>";
          --
          cb

          Comment

          • gosha bine

            #6
            Re: JS/PHP Integration

            On 27.04.2007 16:35 ashore wrote:
            Say I'm using a snippet like
            $phpfoo = "a\nb\nc";
            var jsfoo = "<?php echo $phpfoo;?>";
            >
            This errors out because JS sees the newline, and it needs to be
            escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
            retrieved and displayed OK.)
            >
            What do you folks use in this kind of situation? Thanks, all.
            >
            AS
            >
            try

            <?php echo addcslashes($ph pfoo, "\0..\37"); ?>


            --
            gosha bine

            extended php parser ~ http://code.google.com/p/pihipi
            blok ~ http://www.tagarga.com/blok

            Comment

            • Martin Honnen

              #7
              Re: JS/PHP Integration

              Martin Honnen wrote:
              PHP has addslashes e.g.
              var jsfoo = "<?php echo addslashes($php foo); ?>";
              that should help
              No, it does not help (as it does not esape \n), seems a regular
              expression replacement is needed.



              --

              Martin Honnen

              Comment

              • -Lost

                #8
                Re: JS/PHP Integration

                Martin Honnen wrote:
                Martin Honnen wrote:
                >
                >PHP has addslashes e.g.
                > var jsfoo = "<?php echo addslashes($php foo); ?>";
                >that should help
                >
                No, it does not help (as it does not esape \n), seems a regular
                expression replacement is needed.
                Huh? addslashes would yield a\\nb\\nc. That is effectively escaping
                newlines is it not?

                --
                -Lost
                Remove the extra words to reply by e-mail. Don't e-mail me. I am
                kidding. No I am not.

                Comment

                • Toby A Inkster

                  #9
                  Re: JS/PHP Integration

                  Martin Honnen wrote:
                  PHP has addslashes e.g.
                  var jsfoo = "<?php echo addslashes($php foo); ?>";
                  In this case, addcslashes($ph pfoo) would work better.

                  --
                  Toby A Inkster BSc (Hons) ARCS
                  Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

                  Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

                  * = I'm getting there!

                  Comment

                  • Toby A Inkster

                    #10
                    Re: JS/PHP Integration

                    -Lost wrote:
                    Huh? addslashes would yield a\\nb\\nc. That is effectively escaping
                    newlines is it not?
                    In the given example:

                    $phpfoo = "a\nb\nc";

                    No, it would not. addslashes() wouldn't do anything to that string.

                    --
                    Toby A Inkster BSc (Hons) ARCS
                    Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

                    Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

                    * = I'm getting there!

                    Comment

                    • ashore

                      #11
                      Re: JS/PHP Integration

                      Guys, the addslashes didn't work for me, tho I agree they shd! But
                      tell me why the following PHP didn't wrk:

                      $phpfoo = "\n\n\n";

                      print str_replace('\n ' ,'ZZZZZZZZZZZZZ ' ,$phpfoo);

                      AS

                      Comment

                      • Hendri Kurniawan

                        #12
                        Re: JS/PHP Integration

                        ashore wrote:
                        Guys, the addslashes didn't work for me, tho I agree they shd! But
                        tell me why the following PHP didn't wrk:
                        >
                        $phpfoo = "\n\n\n";
                        >
                        print str_replace('\n ' ,'ZZZZZZZZZZZZZ ' ,$phpfoo);
                        >
                        AS
                        >
                        Because of the single quote.
                        PHP process "\n", but does not process '\n';

                        Hendri

                        Comment

                        • ashore

                          #13
                          Re: JS/PHP Integration

                          Hendri, you're right! I've been PHP'ing for years and didn't know
                          that. I knew in general that PHP wouldn't parse between sgl quotes,
                          but I didn't realize that included this situation. Thanks.

                          AS

                          Comment

                          • Toby A Inkster

                            #14
                            Re: JS/PHP Integration

                            ashore wrote:
                            print str_replace('\n ' ,'ZZZZZZZZZZZZZ ' ,$phpfoo);
                            Stop mucking around with str_replace. Just use addcslashes().

                            --
                            Toby A Inkster BSc (Hons) ARCS
                            Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

                            Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

                            * = I'm getting there!

                            Comment

                            • ashore

                              #15
                              Re: JS/PHP Integration

                              Yep, that works fine. But I need to show line breaks in the html. so
                              the str_replace() is still there.

                              Thanks for all the help here, folks. 'Preciated!

                              AS

                              Comment

                              Working...