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

php in javascript

I have create a checkbox,call getS function in onclick event. I append
value of variable output with value $text. It works well but when I
changed value of $text to a paragraph of text, it will occur error on the
page. Please advise. Thanks.

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

function getS (id){
var output = "";
if (document.getElementById("id").checked)
{
output = output + <? echo $text; ?> ;
}}
</script>
Apr 24 '06 #1
7 1299
You need to learn about the request resonse model. php is run to
produce html(& javascript) which is then sent to the browser. The
broser then runs the html (& javascript). So your code will be
delivered to the browser as

<script language = "javascript">

function getS (id){
var output = "";
if (document.getElementById("id").checked)
{
output = output + testing ; // <- error testing is not a variable.
}}

</script>

Apr 24 '06 #2
Fletch wrote:
You need to learn about the request resonse >model. php is run to
produce html(& javascript) which is then sent >to the browser. The
broser then runs the html (& javascript). So >your code will be
delivered to the browser as <script language = "javascript">
function getS (id){
var output = "";
if (document.getElementById("id").checked)
{
output = output + testing ; // <- error >testing is not a variable.
}}
</script>


Sorry, I have mistype the code, it should be

output = output + "<? echo $text; ?>";

if I assign $text a short text, it will OK. But if $text is a paragraph/
multiple line of string, it will occur error on the page.

Please give idea on what has happen..
Thanks.



Apr 24 '06 #3
You need to have valid javascript code, which is not happening when you
have several lines in text. You want to get to
output +="Line1\nLine2\nLine3\n"; // += is the same as a=a+
so
<? function jsencode($str)
{
return implode('\n',explode("\n",$str)); //Could use str_replace -
probably better.
}?>

output+="<? echo jsencode($text); ?>";

Apr 24 '06 #4

juicy wrote:
it will occur error on the page. Please advise. Thanks.


All your bases are belong to us!

Apr 24 '06 #5
Fletch wrote:
You need to learn about the request resonse >model. php is run to
produce html(& javascript) which is then sent >to the browser. The
broser then runs the html (& javascript). So >your code will be
delivered to the browser as <script language = "javascript">
function getS (id){
var output = "";
if (document.getElementById("id").checked)
{
output = output + testing ; // <- error >testing is not a variable.
}}
</script>


Sorry, I have mistype the code, it should be

output = output + "<? echo $text; ?>";

if I assign $text a short text, it will OK. But if $text is a paragraph/
multiple line of string, it will occur error on the page.

Please give idea on what has happen..
Thanks.



Apr 25 '06 #6
juicy wrote:
Fletch wrote:
You need to learn about the request resonse >model. php is run to
produce html(& javascript) which is then sent >to the browser. The
broser then runs the html (& javascript). So >your code will be
delivered to the browser as


<script language = "javascript">
function getS (id){
var output = "";
if (document.getElementById("id").checked)
{
output = output + testing ; // <- error >testing is not a variable.
}}
</script>

Sorry, I have mistype the code, it should be

output = output + "<? echo $text; ?>";

if I assign $text a short text, it will OK. But if $text is a paragraph/
multiple line of string, it will occur error on the page.

Please give idea on what has happen..
Thanks.



Looks like a javascript problem to me. Try a javascript newsgroup.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 25 '06 #7
Fletch wrote:
You need to have valid javascript code, which >is not happening when you
have several lines in text. You want to get to
output +="Line1\nLine2\nLine3\n"; // += is the >same as a=a+
so
<? function jsencode($str)
{
return implode('\n',explode
("\n",$str)); //Could use str_replace -
probably better.
}?>
output+="<? echo jsencode($text); ?>";

But why if I pass the paragraph of text from onclick event on a checkbox,
to show it on textarea, it can perform well although the text is a
paragraph.

<td ><input type=checkbox name="chksur2"
onclick="getSurcharge('duties',this.value)" value="<? echo $strSurcharge1;
?>">

<script language="JavaScript">
function getSurcharge(id, value)
{
document.getElementById(id).value= value;
//to show text on a textarea
}
</script>

<?php $strSurcharge1 = " (paragraph of text) ";
?>
Apr 25 '06 #8

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

Similar topics

0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.