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

php in javascript

I put a function call in checkbox onclick event, and want to append text to
a variable. But if I get value from a php variable, I get error on page,
but if I put output = output + "text", it works well. Can anyone help?
Thanks

function getS (id){
var output = "";
if (document.getElementById("id").checked)
{
output = output + <? echo $text; ?> ;
}}
Apr 24 '06 #1
9 1169
juicy wrote:
I put a function call in checkbox onclick event, and want to append text to
a variable. But if I get value from a php variable, I get error on page,
but if I put output = output + "text", it works well. Can anyone help?
Thanks

function getS (id){
var output = "";
if (document.getElementById("id").checked)
{
output = output + <? echo $text; ?> ;
}}

Where do you expect the PHP code to be evaluated?

--
Ian Collins.
Apr 24 '06 #2
thanks for your reply. I put the php code above the function. I found that
if $text value is short text, the script perform well, but because of
$text value is a paragraph of text, it caused error occured. But I still
do not know why this error happen.

<script language = "javascript">
<?php $text = "test1"; ?>

function getS (id){
var output = "";
if (document.getElementById("id").checked)
{
output = output + <? echo $text; ?> ;
}}
</script>

Apr 24 '06 #3
juicy wrote:
thanks for your reply. I put the php code above the function. I found that
if $text value is short text, the script perform well, but because of
$text value is a paragraph of text, it caused error occured. But I still
do not know why this error happen.
Please quote context and attributions in your reply, not everyone uses
google.

In what context is this evaluated? Is it in a php file?
<script language = "javascript">
Don't use this, use type="text/javascript".
<?php $text = "test1"; ?>

function getS (id){
var output = "";
if (document.getElementById("id").checked)
{
output = output + <? echo $text; ?> ;
}}
</script>

--
Ian Collins.
Apr 24 '06 #4
Ian Collins wrote:
In what context is this evaluated? Is it in a >php file? Yes, it is a php file.
Don't use this, use type="text/javascript".

Yes, I have changed to it, but the problem still same.
Apr 24 '06 #5
juicy wrote:
Ian Collins wrote:
In what context is this evaluated? Is it in a >php file?


Yes, it is a php file.

Then it's time to pass the ball to the PHP list.

--
Ian Collins.
Apr 24 '06 #6
juicy wrote:
I put a function call in checkbox onclick event, and want to append text
to a variable. But if I get value from a php variable, I get error on
page, but if I put output = output + "text", it works well. Can anyone
help? output = output + <? echo $text; ?> ;


So if $text contains the text: text

Then you are outputting something like:

output = output + text;

Note how this is different to the

output = output + "text";

you had before.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Apr 24 '06 #7
output = output + <? echo (" \' $text \' "); ?>

Apr 24 '06 #8
juicy wrote:
thanks for your reply. I put the php code above the function. I found that
if $text value is short text, the script perform well, but because of
$text value is a paragraph of text, it caused error occured. But I still
do not know why this error happen.
[snip]


There's something you forgot of: line breaks. A paragraph is being
output like that:

output = output + "this is your text

this is your next line";
Which will result in a "unterminated string" error. You must replace,
using PHP, any \n by a literal "\n", which will result:

output = output + "this is your text\nthis is your next line";
Leaving the line-break to be handled by JS.

Apr 24 '06 #9
to*****@gmail.com wrote:
output = output + <? echo (" \' $text \' "); ?>


Not at all.

output += "<?php echo addslashes($text); ?>";
PointedEars
--
In the First World War, 13 million people were killed. In the Second
World War, 40 million people were killed. I think that if a third war
takes place, nothing is going to be left on the face of earth.
-- Shakira, 2003-02-05 @ MTV.com
Apr 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.