How to get textarea value into javascript variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agun
    New Member
    • Oct 2008
    • 16

    How to get textarea value into javascript variable?

    Hello,

    question 1:
    how do i get textarea value to javascript variable if the textarea contains linefeed?
    it only works if the textarea contains no linefeed.

    Code:
    var jtext = document.getElementById('idTextArea').value;

    question 2:
    if i submit that textarea value with POST, how do i get the value? (if the textarea contains linefeed)

    Code:
    $ptext = $_POST['textAreaName'];
    Many thanks...
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    1. Show an example input that it doesn't work with.

    2. It depends on your server-side language, but yeah that should work for PHP.

    Comment

    • agun
      New Member
      • Oct 2008
      • 16

      #3
      The code is a bit long, so this is the problem-part only :


      I have a Contact Us form like this (form action is PHP SELF). The form also has CAPTCHA but i don't show it here (the CAPTCHA is working fine)

      Code:
      <form action="<?php echo $PHP_SELF; ?>" method="post" onsubmit="return convalidate();" >
      
      <textarea id="idconmsg" name="conmsg" cols="50" rows="10" wrap="physical" title="Please type your message"
      		onKeyDown="textCounter(this.form.conmsg,this.form.remLen,500);"
      		onKeyUp="textCounter(this.form.conmsg,this.form.remLen,500);" 
      		></textarea>
      		<br />
      		<input readonly type=text name=remLen size=3 maxlength=3 value="500"> characters left
      Upon submit, it will call javascript function convalidate which is validating all input before submission. (The function itself is working fine)

      And after validation, it will submit to the same page (PHP SELF) which is containing this php code: (i short out the code because it's too long). I pass the textarea content to $conmsg
      Code:
      <?php
      $conmsg=$_POST['conmsg'];
      ?>
      After that, I recheck in PHP whether the CAPTCHA is correct. If correct, I will send the email. If false, the PHP will make a variable $flagcapt=0 that indicates the user needs to re-input the CAPTCHA.
      So, after PHP, i also have javascript to recapture the PHP variable after submission. (The javascript is called on the body load event)

      This is the javascript code (I short it out because it's too long):

      Code:
      function conflag()
      {
      var jflagcapt="<?php echo $flagcapt ?>";
      
                     if(jflagcapt==0)
      		{
      		var jconmsg="<?php echo $conmsg ?>";
                      alert(jconmsg);
                      document.getElementById('idconmsg').value = jconmsg;
      		}
      }
      Actually I already succeeded echo the PHP variable $conmsg and it works. Although with some errors that the line feed characters from textarea is only displayed as whitespace only.

      The problem is, i can't get alert(jconmsg) working. I know that javascript alert can't contain line feed. But, even though I eliminate the alert(jconmsg), the script still error.
      The document.getEle mentById('idcon msg').value = jconmsg;
      just not working if I type some line feed in the textarea.

      But if I type NO LINE FEED at all, it works like a charm.

      So, I assume I have misconception on the LINE FEED thing.
      What should I do?

      Does my explanation clear enough?

      Many thanks acoder,

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The line feed/new line character will cause errors if you set the JavaScript like this:
        Code:
        var jconmsg="<?php echo $conmsg ?>";
        This will translate as
        Code:
        var jconmsg="some text
        and some more
        and more";
        which would obviously cause errors in JavaScript. Either convert to "\n" or set an element directly using PHP and get the value using JavaScript.

        Comment

        • agun
          New Member
          • Oct 2008
          • 16

          #5
          Ooh, i see

          About your solution :

          1) Either convert to "\n"

          You mean, i convert to "\n" in PHP variable $conmsg or in javascript variable jconmsg?
          The problem if i convert to "\n" is, will the email received still contains line feed? Because if not, it will be ugly.



          2) or set an element directly using PHP and get the value using JavaScript.
          How do i do this? I don't understand

          Many thanks...

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by agun
            You mean, i convert to "\n" in PHP variable $conmsg or in javascript variable jconmsg?
            $conmsg
            Originally posted by agun
            2) or set an element directly using PHP and get the value using JavaScript.
            How do i do this?
            If you echo to an element value and then get the value of that element in JavaScript. I'm not sure if this would work (haven't tested), but it wouldn't result in an error.

            Comment

            • agun
              New Member
              • Oct 2008
              • 16

              #7
              I see,

              so what you mean is I need to have somekind of hidden element. And echo from php to that element?

              Okay, I'll try

              Thanks acoder

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Well, that was the second method. You could try the first method.

                I have to ask, though, why you're using JavaScript for this when you can use PHP. In your code, you have:
                Code:
                        var jconmsg="<?php echo $conmsg ?>";
                                alert(jconmsg);
                                document.getElementById('idconmsg').value = jconmsg;
                Why not use PHP instead?

                Comment

                • zaggaustralia
                  New Member
                  • Jun 2010
                  • 1

                  #9
                  thank you for this nice post
                  it's really helpful to me.

                  Comment

                  • Jason Barrett

                    #10
                    Use
                    letthis = myform.mytextar ea.value

                    Make sure you set wrap to either hard, soft or off

                    <textarea name="mytextare a" wrap=hard|soft| off>

                    Comment

                    • Sticks

                      #11
                      @Jason Thanks for that -- thats all i needed! but really needed! thanks

                      Comment

                      Working...