What data type would I use to store javascript code in my PHP program? I'm thinking string, but then there could be " inside the javascript which would then not work when initializing the string. So I'm confused what to use.
$foo="function Loan() { var housePrice; var IR;var dpayment;var years;var monthPay;var month;switch(document.calcform.startm.value) {case "january":document.calcform.startm.value="0";parseInt(document.calcform.startm.value)break;"; //Is what I would like the code to say, but it won't work as a result of the javascript containing a "
So how would I store the javascript code as a datatype to send/recieve via PHP/XML RPC?
Javascript as a string datatype
Moderator: General Moderators
There are only "problems" when a string (literal) is parsed. Not if it is only stored (as variable).
Thereforeis problem, butis not. The same is true for xml-rpc.
Therefore
Code: Select all
$foo="function Loan() { var housePrice; var IR;var dpayment;var years;var monthPay;var month;switch(document.calcform.startm.value) {case "january":document.calcform.startm.value="0";parseInt(document.calcform.startm.value)break;"; //Is what I would like the code to say, but it won't work as a result of the javascript containing a "Code: Select all
<?php
$foo = file_get_contents('data.txt');
?>Code: Select all
// data.txt
function Loan() { var housePrice; var IR;var dpayment;var years;var monthPay;var month;switch(document.calcform.startm.value) {case "january":document.calcform.startm.value="0";parseInt(document.calcform.startm.value)break;"; //Is what I would like the code to say, but it won't work as a result of the javascript containing a-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Your want to output javascript in your PHP source code?
Well a string is the only way, really, I mean I suppose you could use an array of characters or something similar, but that would be overkill...
To prevent your Javascript strings from breaking your PHP enclosed strings you have two options:
1) Use single quotes for all your PHP strings and double quotes for your Javascript strings
2) Escape the " or ' inside your javascript string with \ so...
Notice the \ inside the PHP string, it escapes the double quote intended for your javascript string so the PHP parser reads all in correctly....
Personally I follow the former convention, so simplicity and speed.
Cheers
Well a string is the only way, really, I mean I suppose you could use an array of characters or something similar, but that would be overkill...
To prevent your Javascript strings from breaking your PHP enclosed strings you have two options:
1) Use single quotes for all your PHP strings and double quotes for your Javascript strings
2) Escape the " or ' inside your javascript string with \ so...
Code: Select all
$js_str = "function jsFunc(){ alert(\"Hello from javascript\"); }"Personally I follow the former convention, so simplicity and speed.
Cheers
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Don't forget heredoc syntax.