By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,303 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

Problems Passing PHP var to Javascript Function

P: 5
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:

Expand|Select|Wrap|Line Numbers
  1. <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
Expand|Select|Wrap|Line Numbers
  1. <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.
Sep 5 '08 #1
Share this Question
Share on Google+
7 Replies


Dormilich
Expert Mod 5K+
P: 8,639
you can try
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo '<a href="javascript:doit(' . $tscript . ')"><img src="button.gif"></a>';
  3. ?>
but I'm not sure whether this solves the problem. Further note: maybe the missing quotation marks cause this.

regards
Sep 5 '08 #2

Atli
Expert 5K+
P: 5,058
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.
Expand|Select|Wrap|Line Numbers
  1. // Will work:
  2. doit("value");
  3. doit('value');
  4.  
  5. // Will NOT work (you'r doing this)
  6. doit(value);
  7.  
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)
Sep 5 '08 #3

P: 5
you can try
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo '<a href="javascript:doit(' . $tscript . ')"><img src="button.gif"></a>';
  3. ?>
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.
Sep 5 '08 #4

P: 5
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.
Expand|Select|Wrap|Line Numbers
  1. // Will work:
  2. doit("value");
  3. doit('value');
  4.  
  5. // Will NOT work (you'r doing this)
  6. doit(value);
  7.  
If I substitute that Variable with another one containing a different value it works.

Ex:
Expand|Select|Wrap|Line Numbers
  1. <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......
Sep 5 '08 #5

Atli
Expert 5K+
P: 5,058
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?
Sep 5 '08 #6

P: 5
In the php page:
Expand|Select|Wrap|Line Numbers
  1. <a href=javascript:doit("<?php echo $tscript; ?>")><img src=button.gif> /a>
  2.  
From View Frame source:
Expand|Select|Wrap|Line Numbers
  1. <a href=javascript:testit(' <b>admin - </b> Hello? <br> Hi yourself! <br>')><img src=images/copyto.gif width=22 height=22 border=0></a>
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function doit()
  3. {
  4.      var mytscript = '<?php echo $tscript;?>';
  5.      alert(mytscript);
  6. }    
  7. </script>
  8.  
  9.  
In view frame source:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function doit()
  3. {
  4.      var mytscript = ' <b>admin - </b> Hello? <br> <b>admin - </b>Hi yourself!<br>';
  5.      alert(mytscript);
  6. }    
  7. </script>
  8.  
  9.  
Sep 8 '08 #7

Atli
Expert 5K+
P: 5,058
Ahh ok.

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

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  $message = "<b>What is up?</b><br />Nothing...";
  3. ?>
  4.  
  5. <a href=javascript: alert('<?php echo $message; ?>)>Doit</a><br />
  6. <a href="javascript: alert('<?php echo $message; ?>')">Doit</a>
  7.  
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.
Sep 8 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.