473,237 Members | 1,245 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,237 software developers and data experts.

How can i pass a string with multiple line to a javascript function ?

Hi,

I have a variable in php
Expand|Select|Wrap|Line Numbers
  1. $test = "This is a string
  2. It has multiple lines
  3. there are three total" ;
(Output from a text area)

I want to pass this variable to a javascript function

I have write this in my php file
Expand|Select|Wrap|Line Numbers
  1. <?php echo "<script language=javascript>convert('$test');</script>"; ?>
but this makes an error "Unterminated string literal" How can i solve this issue ??
May 3 '11 #1
1 3901
Atli
5,058 Expert 4TB
Hey.

Unlike PHP, Javascript does not allow strings that span multiple lines.

To do that in Javascript, you need to either do this:
Expand|Select|Wrap|Line Numbers
  1. var str = "First line \n\
  2. Second line \n\
  3. Third line";
  4.  
Or just:
Expand|Select|Wrap|Line Numbers
  1. var str = "First line\n" +
  2.           "Second line\n" + 
  3.           "Third line";
  4.  
To get PHP to generate this, I suggest you look at the str_replace function. The idea would be to replace new lines (\n) with a new-line followed by a back-slash (\n\).
May 4 '11 #2

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

Similar topics

1
by: Obscurr | last post by:
i've got an array created in php that I want to send over to a javascript function. onclick = "return sjekk(<?echo $names; ?> )" in my "sjekk" function, when I try to print an alert : ...
1
by: prince -=nore=- | last post by:
I'm trying to open a new window when a button is clicked. I'm using the following code in my page_load to do so. Dim myScript As String = "<script language=javascript> function loadWin()...
4
by: Nathan Sokalski | last post by:
I am a beginner with AJAX, and have managed to learn how to use it when passing single parameters, but I want to return more than one value to the client-side JavaScript function that displays it....
3
by: Steve Kershaw | last post by:
Hi, I need to pass a value from a hidden input: <input id="hiddentxtWidth" type="hidden" value="1065" /> to a JavaScript function. I've tried the following: <script type="text/javascript" >...
1
by: adpinc | last post by:
Hello all! I am a seasoned programmer, but not really with Javascript. I have a strange phenomenon going on that for the life of me I cannot figure out what is wrong. I am calling a javascript...
4
by: Gotejjeken | last post by:
I have an array of image file names, and I would like to pass them to a JavaScript function for display. Is there anyway I can get the page's codebehind to interact with javascript?
3
by: manojrana | last post by:
I am using php code in a separately file and html code in as separate file , an array is passed from php to smarty, and then passing that array from smarty to javascript, I am getting it as...
7
by: sweetline priya | last post by:
hii friends.. i am in need of a coding.. can u help me?? how to pass a php variable as an argument to javascript function?? is there any possible ways???
4
by: rupalee | last post by:
if($uploadval==0) { $url = 'diacomviewer.php'; print '<a style="cursor:pointer ;font-size:11px; font-weight:lighter; font-family:Arial;" onclick="popitup(\'' .$url....
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.