473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

How to get textarea value into javascript variable?

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
10 91427
acoder
16,027 Expert Mod 8TB
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
agun
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
16,027 Expert Mod 8TB
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
agun
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
16,027 Expert Mod 8TB
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
agun
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
16,027 Expert Mod 8TB
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
thank you for this nice post
it's really helpful to me.
Jun 23 '10 #9
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
@Jason Thanks for that -- thats all i needed! but really needed! thanks
Nov 2 '10 #11

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

Similar topics

2
by: Matt | last post by:
If I assign Java variable a to javascript variable x, it is fine. <% int a = 10; %> var x = <%= a %>; alert(x); But if I do the other way around, then it has 500 error. any ideas??
5
by: ms_chika | last post by:
Hi to all, I have this problem in xsl wherein i want to access a variable in javascript and use it my xsl. How would i access or use a javscript variable in my xsl file? Please help. ...
4
by: tnhoe | last post by:
Hi, if I have both javascript and aspscript, how to get/retrieve the variable value in javascript into asp variable ? regards hoe
3
by: mbasil7 | last post by:
Hi at all! I want to use a javascript variable in php. The reason is that i want to know the client's screen resolution. Keep in mind that i am not a javascript programmer but php. Here is...
1
by: snipersix | last post by:
How do you display values in a textarea using javascript. ex. i have 2 radio buttons o Adult =$9.99 o Child =$6.99 i need it to be, so that if i select adult in the...
2
by: pleaseexplaintome | last post by:
Hi I have the following perl/cgi script snippet. The goal of this script is to pass a javascript variable to perl where it can be re-used later. Any help is appreciated, Thanks ...
5
by: nbt725 | last post by:
Dear Sir, Hello ! I want to get the javascript variable in php code in following function. <script language="JavaScript"> function chg_subcatg(cfield,fieldnm) { // For Retrieval of current...
2
by: buntyindia | last post by:
Hi, I have a session variable msgSize that is being set by a Java file in the session. aRequest.getSession().setAttribute("msgSize", msgSize); It's value is variable depends on record count of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.