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í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ó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í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ón">
</form>
</body>
</html>
Notice this line:
<input name="Submit" type="button"
onClick="MM_popupMsg('this\nis\na\ntest')" value="Botó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.
|