By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

How to get textarea value into javascript variable?

P: 16
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.

Expand|Select|Wrap|Line Numbers
  1. 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)

Expand|Select|Wrap|Line Numbers
  1. $ptext = $_POST['textAreaName'];
Many thanks...
Oct 23 '08 #1
Share this Question
Share on Google+
10 Replies


acoder
Expert Mod 15k+
P: 16,027
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.
Oct 23 '08 #2

P: 16
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)

Expand|Select|Wrap|Line Numbers
  1. <form action="<?php echo $PHP_SELF; ?>" method="post" onsubmit="return convalidate();" >
  2.  
  3. <textarea id="idconmsg" name="conmsg" cols="50" rows="10" wrap="physical" title="Please type your message"
  4.         onKeyDown="textCounter(this.form.conmsg,this.form.remLen,500);"
  5.         onKeyUp="textCounter(this.form.conmsg,this.form.remLen,500);" 
  6.         ></textarea>
  7.         <br />
  8.         <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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $conmsg=$_POST['conmsg'];
  3. ?>
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):

Expand|Select|Wrap|Line Numbers
  1. function conflag()
  2. {
  3. var jflagcapt="<?php echo $flagcapt ?>";
  4.  
  5.                if(jflagcapt==0)
  6.         {
  7.         var jconmsg="<?php echo $conmsg ?>";
  8.                 alert(jconmsg);
  9.                 document.getElementById('idconmsg').value = jconmsg;
  10.         }
  11. }
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.getElementById('idconmsg').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,
Oct 23 '08 #3

acoder
Expert Mod 15k+
P: 16,027
The line feed/new line character will cause errors if you set the JavaScript like this:
Expand|Select|Wrap|Line Numbers
  1. var jconmsg="<?php echo $conmsg ?>";
This will translate as
Expand|Select|Wrap|Line Numbers
  1. var jconmsg="some text
  2. and some more
  3. 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.
Oct 23 '08 #4

P: 16
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...
Oct 23 '08 #5

acoder
Expert Mod 15k+
P: 16,027
You mean, i convert to "\n" in PHP variable $conmsg or in javascript variable jconmsg?
$conmsg
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.
Oct 23 '08 #6

P: 16
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
Oct 23 '08 #7

acoder
Expert Mod 15k+
P: 16,027
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:
Expand|Select|Wrap|Line Numbers
  1.         var jconmsg="<?php echo $conmsg ?>";
  2.                 alert(jconmsg);
  3.                 document.getElementById('idconmsg').value = jconmsg;
  4.  
Why not use PHP instead?
Oct 23 '08 #8

P: 1
thank you for this nice post
it's really helpful to me.
Jun 23 '10 #9

P: n/a
Use
letthis = myform.mytextarea.value

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

<textarea name="mytextarea" wrap=hard|soft|off>
Oct 20 '10 #10

P: n/a
@Jason Thanks for that -- thats all i needed! but really needed! thanks
Nov 2 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.