Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    50
    Thanks
    4
    Thanked 4 Times in 4 Posts

    Unterminate string literal with escaped characters

    This is my first post, so I apologize if I don't give enough, or the correct info the first time.

    I have an AJAX call returning the code to create a form/table that is inserted into a DIV on the calling page. I do pretty well with PHP, but I'm a relative novice with javascript. The called php page creates a TD with an onClick event that calls a javascript function that inserts text into a form's textfield.

    The problem is that I'm running into an "unterminated string literal" error when I click on certain cells. I know that this is caused by unescaped characters, but I don't see what I'm missing. I have escaped all single and double quotes, and there are no line breaks. I don't know how to capture the exact code since it's being returned in an AJAX call, but I've captured the string, after the escaping of quotes, in a variable, then dumped that to the screen to assure that it's being escaped properly.

    PHP code from "called" page:
    Code:
    for ($i=0; $i<$num_comments; $i++) {
    	$newcommenteng[$i] = str_replace ($subfind, $subreplace[english],$comment[$i][english]);
    	$newcommentspan[$i] = str_replace ($subfind, $subreplace[spanish],$comment[$i][spanish]);
    	$englishcomment[$i] = str_replace($manual_find, $manual_replace, $newcommenteng[$i]);
    	$spanishcomment[$i] = str_replace($manual_find, $manual_replace, $newcommentspan[$i]);
    }
    print_r($englishcomment);
    // Generate comment column table DIVs
    $genDiv = "<div><table class='comment_listings'>";
    $behaviorDiv = "<div><table class='comment_listings'>";
    $academicDiv = "<div><table class='comment_listings'>";
    // OUTPUT General Data
    for($i=0; $i<$general_counter; $i++) {
    	$cid = $comment_number[$i][general];
    	$genDiv .="
    		<tr class='comment_row'>
    			<td class='comment_cell' onClick=\"javascript:insertComment('".$englishcomment[$cid]."','".$spanishcomment[$cid]."');\">".$newcommenteng[$cid]."</td>
    		</tr>";
    }
    Javascript code on "calling" page:
    Code:
    function insertComment(eng, span) {
    		parent.document.write_rc_comments.english.value += " "+eng;
    		parent.document.write_rc_comments.spanish.value += " "+span;
    }
    Dump of $englishcomment:

    Array ( [0] => Marcos has completed the sixth grade. He will be moving on to the seventh grade next year. Over the summer, please be sure that Marcos continues to read. This will help him retain some of the skills that we\'ve worked on this year. Good luck during the remainder of your school career Marcos. If you ever need help or advice, I\'d be glad to help in any way I can. Be sure to come back and say, \"hello,\" and let me know how you\'re doing. Enjoy your summer vacation! [1] => This is a test. If it\'s a problem, you\'ll see a code! )


    When I click on the cell containing [0] I get the "unterminated string literal" error. When I click on [1] everything works fine.

    I edited the [1] comment to contain a double-quote (") and I got an unterminated string literal error, but it contained more of a description showing the the problem was in the TD tag at the javascript:insertComment(' point.

    Not sure why the errors would be different, nor why the error with [0] wouldn't show where the issue is coming from.

    Thanks.
    Last edited by FlyingRat; 09-13-2009 at 06:14 AM.

  • #2
    New Coder
    Join Date
    Sep 2009
    Posts
    50
    Thanks
    4
    Thanked 4 Times in 4 Posts

    Got it

    Just learned how to use the htmlspecialchars function. It was strange, though, how it worked. I still had to use str_replace on the strings that were going into the javascript function, then use htmlspecialchars on the result of that, otherwise I still had problems.

    Anyway, it's working now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •