Another issue with processing PHP text into javascript function

Referencing this thread:

I soon discovered that my script only works when a user actually types inside the box.


<script language="javascript" type="text/javascript">
function addtext() {
   var newtext = document.myform.inputtext.value;
   if (document.myform.placement[1].checked) {
      document.myform.outputtext.value = "";
   }
   
   var outText = document.myform.outputtext;
   outText.value += outText.value.length > 0 ? "\
" + newtext : newText;
}
</script>

<form name="myform">
<table border="0" cellspacing="0" cellpadding="5"><tr>
<td><textarea name="inputtext"></textarea></td>
<input type="radio" name="placement" value="append" checked> Add to Existing Text<br>
<td><p><input type="radio" name="placement" value="replace"> Replace Existing Text<br>
<input type="button" value="Add New Text" onClick="addtext();"></p>
</td>
<td><textarea name="outputtext"></textarea></td>
</tr></table>
</form>

My problem is that the contents within the <textarea name=“inputtext”></textarea> has values brought in through my php script.

This works when a user actually types inside the textarea

<textarea name="inputtext"></textarea>

However, when the text is sent through my php script it does not work:

<textarea name="inputtext">
Strawberries
blueberries 
Apples 
Bananas
Oranges
</textarea>

Does anyone have any suggestions for making the javascript addtext function work as a result of contents being filled by php rather then by a user?

I appreciate the advice, thank you.

After your form, add something along these lines:


<script type="text/javascript">

(function() {

if(document.myform.inputtext.value !="") {
addText();
}

})();

</script>

You’re basically creating an anonymous function that automatically executes once it’s reached, checking to see if there is a value in the form field, and if so, calling the addText function.

Hope this helps!