Page 1 of 1

PHP & Javascript "Unterminated string constant"

Posted: Fri Jun 05, 2009 11:49 am
by RedAphex
Basically I have a PHP function that builds an web page from a template file and returns the newly created file's URL as a result. When the user clicks the link on the current page I want it to pass this URL to a Javascript window.open event. The error message listed in this topic's subject is the result of having the Javascript function in the code, and unless I'm mistaken, it suggests that I'm missing a semi-colon or perhaps a quote somewhere. This doesn't appear to be the case as far as I can tell...

The error message only occurs when the Java function in the body is uncommented.

Code: Select all

 
<?php 
    require("basic_lib.php"); 
    
    if( isset($_POST["submitted"]) ){
        @session_start();
        echo "<pre>";
        print_r($_POST);
        add2Cart($_POST,"Automotive");  //Change 'Automotive' to it's variable counterpart when done debugging.
        $url = displayContents("Automotive");
        if( !isset($url) ){
            echo "<b>displayContents did not return a URL result!</b><br>";
            die();
        }
        echo "<b>Functions complete. URL = $url</b><br>";
    }        
?>
<html><head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<link rel="stylesheet" type="text/css" href="imagery_style.css" />
<title></title>
<script SRC="imagery.js" type="text/javascript"></script>
</head><body style="margin:0"><center>
<script type="text/javascript">
    function opencart()
    {
        var oLinkTo = "<?php echo $url; ?>";
        if( oLinkTo != FALSE ){
            window.open(oLinkTo);
        }else{ alert('You have 0 items!'); }
    }
</script>
 
PHP Function (this is only run if items have been added to the session. That function is working perfectly.)

Code: Select all

 
function displayContents($cName)
  {
      $session = session_id();
      $cat = $_SESSION[$session][$cName]["category"];
      $fileList = array_slice($_SESSION[$session][$cat]["files"],0,NULL,TRUE);
      $filename = $_SESSION[$session][$cName]["path"];
      
      $template = fopen("cart.php","rt");
      $fp_create = fopen("$filename","wt");
          
          if( ($template AND $fp_create) )
          {
              @flock($fp_create,LOCK_EX);
              @flock($template,LOCK_SH);
              $i=0;
              $replace = array();
              $anchor = "a name=\"writeFromHere\"";
              
              $replace[$i] = "<form action=\"$filename\" method=\"POST\">\n";
              $i++;
              $replace[$i] = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"2\"><tr align=\"center\" bgcolor=\"black\">\n";
              $i++;
              $replace[$i] = "<tr align=\"center\" bgcolor=\"black\"><td colspan=\"4\"><span>My shopping cart</span></td></tr>\n";
              $i++;
              $replace[$i] = "<tr align=\"center\"><td><span>Item</span></td> <td><span>Size</span></td> <td><span>Price</span></td>".
              "<td><span>Remove</span></td></tr>\n";
              $i++;
              while( $line = fgets($template) ){
                  if( strpos($line,$anchor) ){
                    $line = substr_replace($line,"\n",0);
                    fwrite($fp_create,$line);
                    foreach($fileList as $fname => $size){
                        
                        $replace[$i] = "<tr align=\"center\"><td><span>$fname</span></td>\n";
                        $i++;
                        $replace[$i] = "<td><span>$size</span></td>\n";
                        $i++;
                        
                        switch($size){
                            case "small":
                            $size = "\$1.99";
                            break;
                            
                            case "medium":
                            $size = "\$4.99";
                            break;
                            
                            case "large":
                            $size = "\$9.99";
                            break;
                            
                            case "layered":
                            $size = "\$19.99";
                            break;
                        
                            default:
                            echo "<span class=\"error\">Oops! A problem occurred while retrieving your info. We're sorry for the inconvenience.<br>".
                            "</span>";
                            $_SESSION = NULL;
                            session_destroy();
                            die();
                        }
                        
                        $replace[$i] = "<td><span>$size</span></td>\n";
                        $i++;
                        $replace[$i] = "<td><input type=\"checkbox\" name=\"item_del[]\" value=\"$fname\" /></td></tr>\n";
                        $i++;
                    }
                    $replace[$i] = "</table>\n";
                    $i++;
                    $replace[$i] = "<input type=\"submit\" value=\"Purchase\" /></form>\n";  
                                                                                            
                    foreach($replace as $value){   
                        $line = substr_replace($line,$value,0);
                        fwrite($fp_create,$line);
                    }
                }
                fwrite($fp_create,$line);
              }        
              fclose($fp_create);
              fclose($template);
              $url = $_SESSION[$session][$cName]["path"];
              echo "<b>Page built successfully!</b><br>";
              return $url;
          }else{
              echo "<b>Page build failed!</b><br>";
              return FALSE;
      }
  }
 
I've been wrestling with this for awhile now and if any kind souls out there would assist me I would greatly appreciate it.

Re: PHP & Javascript "Unterminated string constant"

Posted: Fri Jun 05, 2009 12:10 pm
by kaszu
I don't what's the content of $url, but problem could be either new lines or quotes.
URL shouldn't have new lines or quotes, but in case if it does:

In javascript strings can't have new lines (actual line breaks) like in PHP, they must be escaped with backslash. Final code in javascript should be like following

Code: Select all

 
var some_string = '.....\
.......\
........';
or

Code: Select all

 
var some_string = '.....\n.......\n........';
In PHP $str = "\n" is new line, so when outputed to javascript (or html) it becomes an actual NEW LINE (string is split into 2 lines).
You need to replace "\n" with "\\\n" (will result in first example) or with "\\n" (result is second example).

Please post html/javascript when executed, then I will be able to tell where's the problem.

Re: PHP & Javascript "Unterminated string constant"

Posted: Fri Jun 05, 2009 3:08 pm
by RedAphex
Hi Kaszu, thank you very much for your response. It seems you're right about the URL. It's not how it's being set but what it's getting set to. I overlooked the fact that the local server path that the file is being created on will obviously be different than the public URL. Well done sir!