...

View Full Version : pass multiline variable to textarea on onclick event



olivierp
08-31-2008, 08:03 PM
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 :


$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)


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.

Philip M
08-31-2008, 08:41 PM
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.

olivierp
08-31-2008, 08:57 PM
Hi Philip,

thanks for your response.

I changed the populate text function as follows :


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 ?

Philip M
08-31-2008, 09:04 PM
Can you post the text of the variable txt?

A long shot but try:-

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

olivierp
08-31-2008, 09:09 PM
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.

Philip M
08-31-2008, 09:51 PM
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

olivierp
08-31-2008, 10:34 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum