Connecting Tech Pros Worldwide Help | Site Map

php in javascript

juicy
Guest
 
Posts: n/a
#1: Apr 24 '06
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>


fletch
Guest
 
Posts: n/a
#2: Apr 24 '06

re: php in javascript


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>

juicy
Guest
 
Posts: n/a
#3: Apr 24 '06

re: php in javascript


Fletch wrote:[color=blue]
>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[/color]
[color=blue]
><script language = "javascript">
>function getS (id){
>var output = "";
>if (document.getElementById("id").checked)
>{
> output = output + testing ; // <- error >testing is not a variable.
>}}
></script>[/color]

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.







fletch
Guest
 
Posts: n/a
#4: Apr 24 '06

re: php in javascript


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); ?>";

Bart the bear
Guest
 
Posts: n/a
#5: Apr 24 '06

re: php in javascript



juicy wrote:[color=blue]
>it will occur error on the page. Please advise. Thanks.[/color]

All your bases are belong to us!

juicy
Guest
 
Posts: n/a
#6: Apr 25 '06

re: php in javascript


Fletch wrote:[color=blue]
>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[/color]
[color=blue]
><script language = "javascript">
>function getS (id){
>var output = "";
>if (document.getElementById("id").checked)
>{
> output = output + testing ; // <- error >testing is not a variable.
>}}
></script>[/color]

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.







Jerry Stuckle
Guest
 
Posts: n/a
#7: Apr 25 '06

re: php in javascript


juicy wrote:[color=blue]
> Fletch wrote:
>[color=green]
>>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[/color]
>
>[color=green]
>><script language = "javascript">
>>function getS (id){
>>var output = "";
>>if (document.getElementById("id").checked)
>>{
>> output = output + testing ; // <- error >testing is not a variable.
>>}}
>></script>[/color]
>
>
> 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.
>
>
>
>
>
>
>[/color]

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

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
juicy
Guest
 
Posts: n/a
#8: Apr 25 '06

re: php in javascript


Fletch wrote:[color=blue]
>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); ?>";[/color]


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) ";
?>


Closed Thread


Similar PHP bytes