473,396 Members | 2,033 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,396 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 3940
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....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.