Welcome to WebmasterWorld Guest from 54.162.155.183

Forum Moderators: open

Message Too Old, No Replies

JavaScript and PHP help

     
5:23 pm on Jun 11, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



I need to display multiple lines with JavaScript and PHP here is my code.
Header("content-type: application/x-javascript");

//displays an alert box with a php varible $msg passed into it
function echoAlert($msg){

echo "alert('$msg')";

}//echoAlert

$var = ("File Written to Sybase01\\\insurance$\\\MSBCBS\\\Send Log\\\Sending\\nContents:");

//$lines is an array of a multiple line text file
foreach($lines as $i){

//function to be used later
//writeLog($username, $i);
$var .=("\\n {$i}");
}//foreach

echoAlert($var);

This code is executed by another javascript file when the user clicks a button. The problem is my alert box says error missing ")" after argument list. If I manually store multiple lines of var i.e.
$var .= ("\\n {$text1}");
$var .= ("\\n {$text2}");
The alert box works fine.
As I am not a Javascript person, I believe it either has something to do with the way the variable is stored in the array or my escape characters. Any suggestions, thanks in advance.

2:27 pm on Jun 12, 2008 (gmt 0)

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



It could possibly be what is in the array. Try looking at whats in the array and see if it has any characters that need to be escaped. Other than that I am not sure why it is working one way and not the other.
9:50 pm on Jun 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not a big fan of trying to execute a client-side action with a server-side language, so I've broken your script up into server- and client-side sections:

<script type="text/javascript">
function echoAlert(msg){
alert(msg);
}
</script>
<?php
$var = ("File Written to Sybase01\\\insurance$\\\MSBCBS\\\Send Log\\\Sending\\nContents:");
foreach($lines as $i){
$var .=("\\n {$i}");
}
?>
<script type="text/javascript">
echoAlert('<?php echo $var?>');
</script>

This works fine. Your original code did nothing except print out "echo ('File written.." for me.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month