Forum Moderators: open

Message Too Old, No Replies

PHP/JavaScript combination question

         

andrewsmd

2:36 pm on Jun 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a JavaScript function that calls an external PHP file.
Here is my JavaScript
function loadFile(targetFile)
{
var elem = document.createElement("script");
elem.setAttribute("src", targetFile);
document.getElementsByTagName("body")[0].appendChild(elem);
}
Then in my HTML I have a button
<input type = "button" name = "button" onclick="loadFile('fileName.php')">

Then in my PHP file I output everything with JavaScript. An example is something of this nature.

Header("content-type: application/x-javascript");
if("some condition here"){\
$message = "True";
echo("alert('$message')");
}
else{
$message = "False";
echo("alert('$message')");
}
Now that will display a JavaScript alert box with the statement. It works just fine until I put my variable in a for loop. i.e.

//returns an array with each file that you specify
//ending with a .txt extension
$var = glob("c:\folder\*.txt");
foreach ($var as $i){

$message .= "{$i}\\n";

}//foreach
echo("alert('$message')");
if I do that I get a JavaScript error saying it is an unterminated string literal. However if I manually do it, it works fine. i.e.
$msg .="test\\n";
$msg .="test2\\n";
echo("alert('$msg')");
Any suggestions? Thanks,

eelixduppy

5:55 pm on Jul 1, 2008 (gmt 0)



Echo out the contents of $message and see what it contains. You should be able to see any errors if they exist. Also, you should check the generated source code from this script as well, as it might give you some clues as to where it is screwing up.

One more thing. You should be getting JUST the PHP script working at this time. Forget the asynchronous call to this script unless it is working just fine :)

andrewsmd

1:41 pm on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know the PHP works fine because when I run it as straight PHP i.e. I only have PHPcode and go to that webpage, I get the exact output I need. I'm not a beginner in PHP, but I am in JavaScript. I think I may have discovered my problem though. What I am trying to output is a text file that PHP writes to an alert box. Here is how I read the file.

//open the file just pretend $fname is a valid path for a file
$file = fopen($fname, 'a');

//store the file into an array
$lines = file($fname);

foreach($lines as $i){
fwrite($file, "{$i}");
$msg .= "{$i}";
}//foreach

now if I var_dump($msg) here is my exact output.
"$LANG = "VBScript"
' VBS script learned for "ANSI - MS BCBS.HAW" on 03/09/06 at 08:50:23.

cr = Chr(13)

haAbortOnError

haWaitForPrompt "gon Id: "
haTypeText "S2176"&cr

haWaitForPrompt "ssword: "
haTypeText "ECLAIM"&cr

haWaitForPrompt cr&Chr(10)&cr&"cmd> "

haTypeText "$$ADD ID=S2176FFW BID='PANSI837P'"&cr

haSetXferProtocol 1, 8
haXferAddToSendList "\\sybase01\insurance$\MSBCBS\MSBCBS_20080701_170203_.edi"
haWaitForPrompt "7fed4"&cr&Chr(10)&Chr(17)
haXferSendFromList

haWaitForPrompt cr&Chr(10)&cr&"cmd> "
haTypeText "$$ADD ID=S2176FUZ BID='PANSI837P'"&cr

haSetXferProtocol 1, 8
haXferAddToSendList "\\sybase01\insurance$\MSBCBS\MSBCBS_20080701_170203_1.edi"
haWaitForPrompt "7fed4"&cr&Chr(10)&Chr(17)
haXferSendFromList

haWaitForPrompt cr&Chr(10)&cr&"cmd> "
haTypeText "$$ADD ID=S2176DQR BID='PANSI837P'"&cr

haSetXferProtocol 1, 8
haXferAddToSendList "\\sybase01\insurance$\MSBCBS\MSBCBS_20080701_195038_.edi"
haWaitForPrompt "7fed4"&cr&Chr(10)&Chr(17)
haXferSendFromList
haWaitForPrompt cr&Chr(10)&cr&"cmd> "
haTypeText "$$LOGOFF"&cr

haTerminate"

could my string problem be because the text file has " in it. And if so any ideas on how to fix it. I would need to escape the " but I need to escape them for the JavaScript output, not for the PHP. Thanks,