Google Answers Logo
View Question
 
Q: javascript variable manipulation ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: javascript variable manipulation
Category: Computers > Programming
Asked by: mhffs-ga
List Price: $10.00
Posted: 18 Feb 2003 11:58 PST
Expires: 20 Mar 2003 11:58 PST
Question ID: 163081
Hi,
I have a large block of text coming from a MySQL database into php,
then passed from php as a variable onto a javascript variable on an
html page (templates).
  
If the text is one long line the page displays it perfectly, however
if the text has hidden characters (/n, etc...) the text loads
improperly into the javascript variable.  Instead it either freezes or
loads only to the first line break.

I would like to learn how to load this properly into the javascript
variable.  I am not able to manipulate the variable at the php stage
since I am using codeCharge (if you are familiar with codeCharge you
may be able to assist at the php stage instead).

Thanks for your help,
Martin

Clarification of Question by mhffs-ga on 18 Feb 2003 12:17 PST
This is for a speedreading application at
http://www.universityresource.com/SpeedReader.php
Answer  
Subject: Re: javascript variable manipulation
Answered By: joseleon-ga on 19 Feb 2003 02:37 PST
Rated:5 out of 5 stars
 
Hello, mhffs:
 
 Your problem is very common on PHP+JS development, the trick here is
to replace the *hidden* chars by their representation in javascript
when you are producing the javascript code.
 
 In the example below (generated with DreamWeaver MX, just for
simplicity), you can see how to avoid this problem:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Documento sin t&iacute;tulo</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_popupMsg(msg) { //v1.0
  alert(msg);
}
//-->
</script>
</head>
<?php
	$var="this\nis\na\ntest";
?>
<body>
<form name="form1" method="post" action="">
  <input name="Submit" type="button" onClick="MM_popupMsg('<?php echo
str_replace("\n","\\n",$var); ?>')" value="Bot&oacute;n">
</form>
</body>
</html>

The key is here:

<?php echo str_replace("\n","\\n",$var); ?>

This line, replaces the \n chars found in $var, by a \n on the
javascript code. If you use a double slash (\\) you are indicating php
to produce only one slash (\), so the resulting code of executing this
script is this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Documento sin t&iacute;tulo</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_popupMsg(msg) { //v1.0
  alert(msg);
}
//-->
</script>
</head>
<body>
<form name="form1" method="post" action="">
  <input name="Submit" type="button"
onClick="MM_popupMsg('this\nis\na\ntest')" value="Bot&oacute;n">
</form>
</body>
</html>

Notice this line:

<input name="Submit" type="button"
onClick="MM_popupMsg('this\nis\na\ntest')" value="Bot&oacute;n">

I don't have used CodeCharge, but it must provide some kind of
functions/events to manipulate strings before output. If you need
further research on this matter, I will download the trial version.
Don't hesitate to request for any clarification.

Regards.

Request for Answer Clarification by mhffs-ga on 20 Feb 2003 07:18 PST
if I run it like this it should work right?

<Javascript>
.
.
.
var str = "<?php str_replace("\n"," ",{Article}); ?>";
.
.
Where the {Article} is a php variable coming in from a php page to the
template page.

Clarification of Answer by joseleon-ga on 20 Feb 2003 08:42 PST
Hello:
  I supose that code comes from a CodeCharge template, I don't know
the way it behaves, but it will replace \n by spaces.

What problems do you have with it?

Regards.

Request for Answer Clarification by mhffs-ga on 20 Feb 2003 09:38 PST
The variable {Article} passes successfully into the html, but when I
place it in php <> the variable passes as null???

Any thoughts?

Clarification of Answer by joseleon-ga on 20 Feb 2003 10:30 PST
Hello:
  This seems to be an specific behaviour of CodeCharge's template
system, I will try to download the trial version and check it out. By
the way, you have to think that {XXXXX} is parsed by CodeCharge to
generate your code, so keep trying combinations, for example:

<?php echo "?>{article}<?php "; ?>

and so on. Also check out the help of CodeCharge to read how the
template system works.

Regards.

Request for Answer Clarification by mhffs-ga on 20 Feb 2003 10:49 PST
Thanks,
I am trying different combinations glad to have your help with this,
we'll top up the payment.

Clarification of Answer by joseleon-ga on 21 Feb 2003 01:47 PST
Hello:
  I'm playing right now with CodeCharge, you cannot type the code this
way:

var str = "<?php str_replace("\n"," ",{Article}); ?>"; 

Because the javascript code is in a template, so the php is not
executed there. One fast solution is to generate the project to not
use templates, that way, the code is produced entirely from PHP. With
templates, you must format the variable Article in the php code
"before" is replaced in the template. I keep investigating how to do
it, I'm new to CodeCharge ;-)

Regards.

Clarification of Answer by joseleon-ga on 21 Feb 2003 02:29 PST
Hello, mhffs:

I have been playing more with CodeCharge, and you can replace any
variable on the template just before being parsed using this line:

$Tpl->SetVar('MYMESSAGE',str_replace("\n","\\n","hello,\nWorld"));

MYMESSAGE is {MYMESSAGE} on the template, and in the php code of your
page, this line must be placed after load the template:


//Initialize HTML Template @1-A0111C9D
$CCSEventResult = CCGetEvent($CCSEvents, "OnInitializeView");
$Tpl = new clsTemplate();
$Tpl->LoadTemplate(TemplatePath . $TemplateFileName, "main");
$Tpl->SetVar('MYMESSAGE',str_replace("\n","\\n","hello,\nWorld"));

I have sent you the full source code to your mail account. If you
provide me a short sample of what you are doing, I could try to fix
it.

Regards.
mhffs-ga rated this answer:5 out of 5 stars and gave an additional tip of: $5.00
Thanks for the help.  We have not solved this issue but want to get a
rating in.  If you have any further results please send them to
martin.holmes@universityresource.com

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy