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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Canada
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    pass multiline variable to textarea on onclick event

    Hi all,

    This is my first post here.

    code flow : I'm writing javascript via php to be able to add a php varaible (obtained from a mysql_query) into a javascript varaible. That js var is used in an onclick event on an a href tag.
    When I open the page, I get a js error (unterminated string constant).
    When I clicked the a href, I get an undefined var error.
    clicking any other a href links that are not multiline works fine without errors.


    I have looked for similar issues as the one I'm having and found different posts, however, all the str_replace and ereg did not work for that situation.

    Any help would be greatly appreciated as I have been on this for too many hours now ; )

    Here is the code that assigns the php var to the js var :

    Code:
    $client_ts = mysql_query("SELECT * FROM ts WHERE cat_id = '".$selId."' && type = 'cli' ORDER BY seq ");
    	$j = 1;
    	while($row = mysql_fetch_array($client_ts, MYSQL_ASSOC)){
    		
    		echo '<script type="text/javascript">';
    		echo "var client_ts".$j."='".$row['clicked_value']."';"; 
    		echo "var client_check".$j."='client_check".$j."';";
    		echo '</script>';
    		echo '<tr><td>';	
    		echo "<a href = 'javascript:void(0)' onclick='populate_txt(client_ts".$j.",client_check".$j."); limitText(document.form1.notepad,document.form1.countdown,1000); trackClicks(client_ts".$j.");'  STYLE='text-decoration: none' title='".$row['clicked_value']."' >".$j." . ".$row['name']."</a>";
    		echo '</td>';
    		echo '<td align="right">';
    		echo '<span id= "client_check'.$j.'" style="visibility:hidden">';
    		echo '<img src="images/check.gif">';
    		echo '</span></td><tr>';
    	
    		$j++;
    		
    	}
    Here is the onclick function : (where notepad is the textarea)

    Code:
    function populate_txt(txt,checkmark){
    	
    	var memo = document.getElementById('notepad').value + txt +'\n';
    	
    	if(memo.length > 1000){
    		var loss = memo.length - 1000;
    		alert ('You have exceeded the maximum amount of characters for Lynx.');
    	}else{
    		document.getElementById('notepad').value = memo;
    		document.getElementById(checkmark).style.visibility = 'visible';
    	}
    }
    I hope this information is enough.. thanks for your help.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Try:-

    str = str.replace(/\s+/g," "); // replace all line feeds, carriage returns, tabs and spaces by a single space.



    Seen in the classified ads: Nice parachute - never opened - used once.

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Canada
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Philip,

    thanks for your response.

    I changed the populate text function as follows :

    Code:
    function populate_txt(txt,checkmark){
    	
    	txt = txt.replace(/\s+/g," "); 
    	var memo = document.getElementById('notepad').value + txt +'\n';
    	
    	if(memo.length > 1000){
    		var loss = memo.length - 1000;
    		alert ('You have exceeded the maximum amount of characters for Lynx.');
    	}else{
    		document.getElementById('notepad').value = memo;
    		document.getElementById(checkmark).style.visibility = 'visible';
    	}
    }
    But I still get the same error unterminated string constant.

    Any other suggestions ?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Can you post the text of the variable txt?

    A long shot but try:-

    txt = txt.replace(/\s+/gm," ");

  • #5
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Canada
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sure :

    Of course the txt variable changes based on the db entry.

    when picking up the value of txt in the source code for the first multiline entry, it looks like this :

    'Checked RJ-11 (note the linebreak here which shows up as a hard break in the source code)
    Removed splitters'

    Of course, what is in () is not part of the value.

    I know js is giving the error because of the line break, but I'm unable to get rid of them..

    thanks again

    I'll try the line you mentionned above..
    EDIT :
    I tried that line, same issue still.
    Last edited by olivierp; 08-31-2008 at 08:12 PM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    OK. Try stripping the LF/CR with PHP:

    $txt = str_replace('\r', '', str_replace('\n', '', $txt));

    or:-


    $txt = str_replace(chr(10), " ", $txt); //remove line feeds
    $txt = str_replace(chr(13), " ", $txt); //remove carriage returns


    or in Javascript:-

    var variable = "<?php echo str_replace('\n', '\\n', $variable); ?> so that the literal string ‘\n’ is output instead of an actual line break.


    It is your responsibility to die() if necessary… - PHP Manual
    Last edited by Philip M; 08-31-2008 at 09:09 PM.

  • Users who have thanked Philip M for this post:

    olivierp (08-31-2008)

  • #7
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Canada
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    great stuff !!!

    well Philip, you have done it...

    This is what worked for me :

    str_replace(chr(13),'\\n',str_replace(chr(10),'',$row['clicked_value']))

    In this case, I am replacing only one of the line break types as I still want to display the line breaks in the textarea.

    I am forever greatfull Philip.


  •  

    Posting Permissions

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