SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Another issue with processing PHP text into javascript function...

    Referencing this thread:

    http://www.sitepoint.com/forums/show...=1#post4636792

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

    Code:
    <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 ? "\n" + 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
    Code:
    <textarea name="inputtext"></textarea>
    However, when the text is sent through my php script it does not work:
    Code:
    <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?

  2. #2
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After your form, add something along these lines:

    Code:
    <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!

  3. #3
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I appreciate the advice, thank you.


Bookmarks

Posting Permissions

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