Problems Passing PHP var to Javascript Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geneticus
    New Member
    • Sep 2008
    • 5

    Problems Passing PHP var to Javascript Function

    I have a page containing tabs that are generated through a loop.
    Each iteration through the loop the tab contains several links that preform functions specific to the contents of that tab (chat pages)

    I have a function in PHP that gathers all of the chat messages for a given tab and smooshes (technical term :P) them together and dumps them into a js function which in turn allows the transcript to be copied to the clipboard with a single button click. The problem I am running into is that when I issue:

    Code:
    <a href=javascript:doit(<?PHP Echo $tscript; ?>)><img src=button.gif> /a>
    the page shows an error in the status bar: javascript:doit (

    The page also displays the contents of the string as a link "somerandomtext )>"
    it looks as if the php script is stopping after the ? in the ?> closing tag.
    I can pass another variable in the same script via an echo to js and it stays hidden on the page and allows the function to work.
    I think there is something in this string that needs to be removed (a special char or tag) but I am at a loss as to what would be in there.
    The transcript is gathered from a database that inserts
    Code:
    <b>speaker: </b> text from the query return <br>
    into the string in a loop until all lines are processed.

    I have tried removing all of the HTML tags in the string before passing it to the js function and that didn't resolve the issue with the braces staying open.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you can try
    [CODE=php]<?php
    echo '<a href="javascrip t:doit(' . $tscript . ')"><img src="button.gif "></a>';
    ?>[/CODE]
    but I'm not sure whether this solves the problem. Further note: maybe the missing quotation marks cause this.

    regards

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      Yea, there should be quote marks around the value passed to your JavaScript function. Otherwise the parser will assume that the value is a *variable* name.
      [code=javascript]
      // Will work:
      doit("value");
      doit('value');

      // Will NOT work (you'r doing this)
      doit(value);
      [/code]

      If the value does contain quote-marks, then you can either escape them using the addslashes function, or convert them into HTML entities using the htmlentities function. (Make sure to check out the second parameter)

      Comment

      • Geneticus
        New Member
        • Sep 2008
        • 5

        #4
        Originally posted by Dormilich
        you can try
        [CODE=php]<?php
        echo '<a href="javascrip t:doit(' . $tscript . ')"><img src="button.gif "></a>';
        ?>[/CODE]
        but I'm not sure whether this solves the problem. Further note: maybe the missing quotation marks cause this.

        regards

        This passes the variable Name as a string, not the variable Contents.

        Comment

        • Geneticus
          New Member
          • Sep 2008
          • 5

          #5
          Originally posted by Atli
          Hi.

          Yea, there should be quote marks around the value passed to your JavaScript function. Otherwise the parser will assume that the value is a *variable* name.
          [code=javascript]
          // Will work:
          doit("value");
          doit('value');

          // Will NOT work (you'r doing this)
          doit(value);
          [/code]
          If I substitute that Variable with another one containing a different value it works.

          Ex:
          Code:
          <a href=javascript:doit(<?PHP Echo $channel_A['onchannel']?>)><img src=button.gif></a>
          That works giving me the result of javascript:doit (23)
          while there are quotes there, they are inside the PHP tag fore array retrevial.

          Since it's just the contents of that one variable that gives me this issue, that leads me to believe there is a special character there in there that I am not seeing......
          Last edited by Atli; Sep 5 '08, 09:51 PM. Reason: Fixed the quote

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            What is the content of the PHP variable you are passing into your JavaScript function?

            If it is a number, like in the example you posted, it should be without quotes. If it is a string, it should be quoted. (In the JavaScript code, not the PHP code)

            If this is caused by some special char you should be able to see that in the source. How does the actual call look after it has been created by PHP?

            Comment

            • Geneticus
              New Member
              • Sep 2008
              • 5

              #7
              In the php page:
              Code:
              <a href=javascript:doit("<?php echo $tscript; ?>")><img src=button.gif> /a>
              From View Frame source:
              Code:
              <a href=javascript:testit(' <b>admin - </b> Hello? <br> Hi yourself! <br>[B]')>[/B]<img src=images/copyto.gif width=22 height=22 border=0></a>
              and I get the same result.

              However, If instead of passing it through a js function, I pass it within the function(below) , it works. The problem with this is that I only get the last tab's value of $tscript as tabs are generated in a loop.

              In php:
              Code:
              <script type="text/javascript">
              function doit()
              {
                   var mytscript = '<?php echo $tscript;?>';
                   alert(mytscript);
              }	
              </script>
              In view frame source:

              Code:
              <script type="text/javascript">
              function doit()
              {
                   var mytscript = ' <b>admin - </b> Hello? <br> <b>admin - </b>Hi yourself!<br>';
                   alert(mytscript);
              }	
              </script>
              Last edited by Atli; Sep 8 '08, 06:33 PM. Reason: Fixed the closing [/code] tags.

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Ahh ok.

                Try quoting the entire href. Element attributes should always be quoted anyways. Otherwise you risk running into problems like these.

                For example:
                [code=php]
                <?php
                $message = "<b>What is up?</b><br />Nothing...";
                ?>

                <a href=javascript : alert('<?php echo $message; ?>)>Doit</a><br />
                <a href="javascrip t: alert('<?php echo $message; ?>')">Doit</a>
                [/code]
                The first link, not being quoted, will be parsed as a part of the HTML. So any < or > characters in the attributes will be parsed as normal HTML elements (even tho they are invalid).

                But the second one will work. Even tho it contains < and >, it's quoted so the parser should understands that they belong to the href attribute and it will ignore them.

                Comment

                Working...