Outputting variables in javascript confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andy.z@blueyonder.com

    Outputting variables in javascript confusion


    I'm confused -

    The following outputs a URL encoded string into an alert box
    ----------------------CUT ----------------------
    function createEmbarrasm ent() {
    $tmp = urlencode("Your fly is undone!");
    ?<script type="text/javascript"><!--
    alert('<?php echo $tmp; ?>')
    //--</script>
    <?php
    }
    -------------------------------
    Unfortunately displaying the URL encoded version of the string

    If you remover the URL encoding the alert box doesn't even display

    Next

    Using constants - Again this fails to even open the alert box
    ----------------------CUT ----------------------

    function createEmbarrasm ent() {
    define("TEMP"," Your fly is undone!");
    ?<script type="text/javascript"><!--
    alert('<?php echo TEMP; ?>')
    //--</script>
    <?php
    }
    -------------------------------

    AND YET

    ----------------------CUT ----------------------
    function createEmbarrasm ent() {
    ?<script type="text/javascript"><!--
    alert('<?php echo PHP_VERSION; ?>')
    //--</script>
    <?php
    }
    -------------------------------
    Opens the box and displays fine without URL encoding

    MY PROBLEM - I'm trying to do the first example without the display
    showing the URL encoding.


    Can anyone explain or help please?

    -andy-

  • Rik

    #2
    Re: Outputting variables in javascript confusion

    <andy.z@blueyon der.comwrote:
    The following outputs a URL encoded string into an alert box
    URL encoding is for, well URL's, not for text, prompt or alerts. Drop
    that, unless you want to alert(?) how urlencoding looks like.
    If you remover the URL encoding the alert box doesn't even display
    Does here, as long as $tmp is set.
    We're not in a javascript group here, and all your issues are related to
    javascript itself. Wether this is dynamically build or hardcoded does not
    matter to the browser (heck, it doesn't even know it).

    Just look at the _source_ of you generated HTML-document, check what's
    wrong with the javacript, and adjust the output oh PHP accordingly.
    --
    Rik Wasmus
    Posted on Usenet, not any forum you might see this in.
    Ask Smart Questions: http://tinyurl.com/anel

    Comment

    • Jeff North

      #3
      Re: Outputting variables in javascript confusion

      On Fri, 09 Mar 2007 17:50:48 GMT, in comp.lang.php
      andy.z@blueyond er.com
      <MPG.205baf093b e9d698997d@news-text.blueyonder .co.ukwrote:
      >|
      >| I'm confused -
      >|
      >| The following outputs a URL encoded string into an alert box
      >| ----------------------CUT ----------------------
      >| function createEmbarrasm ent() {
      >| $tmp = urlencode("Your fly is undone!");
      >| ?<script type="text/javascript"><!--
      >| alert('<?php echo $tmp; ?>')
      >| //--</script>
      >| <?php
      >| }
      >| -------------------------------
      >| Unfortunately displaying the URL encoded version of the string
      >|
      Javascript alert boxes are plain text. So if you use
      alert("This is line 1<br />This is line 2");
      then This is line 1<br />This is line 2 will be displayed

      To show the above correctly you need
      This is line 1\nThis is line 2

      Change your $tmp variable to
      $tmp = "Your fly is undone!";
      ---------------------------------------------------------------
      jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
      ---------------------------------------------------------------

      Comment

      Working...