PHP Varibles to Javascript for repeating textareas

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
XF|Fatalpixel
Forum Newbie
Posts: 2
Joined: Wed May 12, 2004 12:56 pm

PHP Varibles to Javascript for repeating textareas

Post by XF|Fatalpixel »

I am having a problem.

Here is a javascript I have adapted. Its from NOWv1.2.3 JS TextControls.

Code: Select all

function insertAtCursor(myField, myValue) 
{
  //IE support
  if (document.selection) 
  {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') 
  {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
                  + myValue 
                  + myField.value.substring(endPos, myField.value.length);
  } 
  else 
  {
    myField.value += myValue;
  }
}

browser = navigator.appName;
if (browser == "Netscape")
{
	function format(from_php,format1, format2)
	{
  		var text = prompt('Enter text to format:','');
  		if (text != null)
 		{
  			getSel();
insertAtCursor(document.edit.text1, format1 + text + format2);
  		}
 		 else
  		{
  			return;
  		}
	}	
}
else 
{
	function getSel()
	{
		if (document.selection) txt = document.selection.createRange().text;
		else return;
		return txt;
	}

	function format(from_php,format1, format2)
	{
		getSel();
  		insertAtCursor(document.edit.text1, format1 + txt + format2);
	}	

}
Currently it only works on the textarea: document.edit.text1. I need it to be applied to each of the text boxes which are constructed thus:

Code: Select all

<?
####################################################################################
####################################################################################
####################################################################################

for($i = 1 ; $i < 31 ; $i++):

	$i_name_var = 'TC' . $i . '_NAME';
	$i_desc_var = 'TC' . $i . '_DESC';
	?>
	
	<tr><td colspan="2">&nbsp;</td></tr>

	<tr valign="middle">

		<td width="100">Name:</td>
		<td><input type="text" name="<?=$i_name_var?>" class="t" style="width:90%;" value="<?=$cArr&#1111;0]&#1111;$i_name_var]?>"></td>
	
	</tr>

	<tr valign="top">

		<td>Description:</td>
		<td><p>
		  <textarea name="<?=$i_desc_var?>" style="width:90%;" rows="7" class="t"><?=$cArr&#1111;0]&#1111;$i_desc_var]?>
		
		  </textarea>
		<br><input type="button" name="bold" Value=" b " OnMouseDown='format($i_desc_var,"<b>", "</b>")'>
<input type="button" name="italics" Value=" i " OnMouseDown='format($i_desc_var,"<i>", "</i>")'>
<input type="button" name="underline" Value=" u " OnMouseDown='format($i_desc_var,"<u>", "</u>")'></p>
As you can see, (hopefully), I need to pass the variable: $i_desc_var back to the javascript so it knows where to insert the tags.

I am totally lost how I can dynamically assign the correct textarea. Still a newb to be honest. I have a vague feeling that something like: document.edit.eval(from_php) might be the key.

So in summary I need the PHP to pass the variable '$i_desc_var' back to the javascript so it effectively writes:
insertAtCursor(document.edit.$i_desc_var , format1 + text + format2);
Post Reply