473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:

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
7 2647
Dormilich
8,658 Expert Mod 8TB
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
5,058 Expert 4TB
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
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
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
5,058 Expert 4TB
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
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
5,058 Expert 4TB
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

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

Similar topics

1
by: Patrice | last post by:
Hi, I'm trying to use a javascript function passing a vb variable. I don't understand why it doesn't work. Can someone help me? Thanks in advance. Here is my javascript function used to...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
4
by: Reply Via Newsgroup | last post by:
Folks, I have a simple script below... I come from a programming background with PHP,C++,bash unix shell scripting so I have a rough understanding when it comes to javascript. I have written a few...
2
by: Sharon | last post by:
Hi y'all, I'm trying to create a customized context menu using the DHTML Popup object. The HTML that makes up the body of the popup is created in a variable named 'popCode'. Works like a charm,...
1
by: Kirk Soodhalter | last post by:
Hi, I am relatively new to javascript, and I am having issues with using the document.getElementById function. I am building a class and in the constructor, I associate the object to an element...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
10
by: Danny | last post by:
Hi all, I am having some odd problems with AJAX on Firefox (1.5). When I use GET as the request method everything works ok, but when I do a POST the remote function doesn't get the parameters I...
4
by: allanrodkin | last post by:
Hi, I'm designing a website in dreamweaver and I'm using JavaScript to slide text across the page. The text is contained in <div> tags. I have designed a function which can move two of the div...
5
by: moni | last post by:
Hi.. I am trying to use javascript for google maps display. If I call the javascript function from my aspx file I use: <input type="text" id="addresstext" value="Huntington Avenue,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.