not getting returns with javascript grab of textarea

D

dan glenn

hi. I'm having a problem using javasript to pass the value of a textarea (in
a form) to a PHP script file.

I want to code a 'preview' function into a guestbook entry page, using an
HTML link with some javascript to grab the form's textarea value, and open
another browser window that uses a php file that applies all the styles,
etc. to the contents of what was in the text area (the guestbook entries
allow simple HTML) and show the preview that way.

For the 'Preview' link i have:

<a href="#" onClick="popawindow(500,400,'previewgbmsg.php?t='
+window.document.signupform.message.value);return false;">Preview</a>

Note that 'popawindow' is just a javascript program I use that opens another
browser window (of size 500x400 here), using the 'previewgbmsg.php' PHP
file, with the GET argument that is passing the textarea's value text to the
PHP file to process.

Now, all this works just fine, EXCEPT that the hard returns in the textarea
are not coming over.

I know that in pure javascript, if i do:

Alert(window.document.signupform.message.value);

i will see all hard returns that may have been in the textarea in the alert
message window. So one would THINK that the object value expression SHOULD
be returning hard returns, at least in some way (i haven't a clue how) in my
t='+window.doc...etc expression.

Anyway, I'm not getting them. I get everything else. (Makes no difference
the WRAP setting in the Textarea tag, by the way). Is there a way I can pass
a <Textarea>'s current value to a PHP script file, included any hard returns
it may have? Is there some way I can "pre-process" the textarea value so I
can put it into the 'get' for my php file?

thanks,
-dg
 
B

Brynn

Will hard returns passs in a querystring? I am not sure, haven't
tested ... in any case, try replacing them with @@@ and switching them
back on the other page.
 
M

Michael Winter

For the 'Preview' link i have:

<a href="#" onClick="popawindow(500,400,'previewgbmsg.php?t='
+window.document.signupform.message.value);return false;">Preview</a>

Note that 'popawindow' is just a javascript program I use that opens
another
browser window (of size 500x400 here), using the 'previewgbmsg.php' PHP
file, with the GET argument that is passing the textarea's value text to
the
PHP file to process.

Now, all this works just fine, EXCEPT that the hard returns in the
textarea
are not coming over.

You can't have them in URIs - that's why. If you submitted the data as
part of a form (and I'd recommend changing to POST if you did that), the
new-lines would be escaped automatically. However, because you're creating
the URI, that conversion isn't performed.

Simply call escape() on the the value before appending it. That is:

'previewgbmsg.php?t=' + escape( window.document.signupform.message.value
)

PHP can unescape the string with urldecode().

An aside: use the collection syntax to reference form controls - it will
allow your code to work across more browsers. The above reference should
be written as:

document.forms['signupform'].elements['message'].value

Mike
 
D

dan glenn

Michael - You da MAN!!!

Works beautifully. Thanks a lot, also for the tip on the 'collection
syntax'.

-dg

Michael Winter said:
For the 'Preview' link i have:

<a href="#" onClick="popawindow(500,400,'previewgbmsg.php?t='
+window.document.signupform.message.value);return false;">Preview</a>

Note that 'popawindow' is just a javascript program I use that opens
another
browser window (of size 500x400 here), using the 'previewgbmsg.php' PHP
file, with the GET argument that is passing the textarea's value text to
the
PHP file to process.

Now, all this works just fine, EXCEPT that the hard returns in the
textarea
are not coming over.

You can't have them in URIs - that's why. If you submitted the data as
part of a form (and I'd recommend changing to POST if you did that), the
new-lines would be escaped automatically. However, because you're creating
the URI, that conversion isn't performed.

Simply call escape() on the the value before appending it. That is:

'previewgbmsg.php?t=' + escape( window.document.signupform.message.value
)

PHP can unescape the string with urldecode().

An aside: use the collection syntax to reference form controls - it will
allow your code to work across more browsers. The above reference should
be written as:

document.forms['signupform'].elements['message'].value

Mike
 
M

Michael Winter

Michael Winter said:
An aside: use the collection syntax to reference form controls - it will
allow your code to work across more browsers. The above reference should
be written as:

document.forms['signupform'].elements['message'].value

Like a nightmare that won't end, its me again.

You like picking on me, don't you*. :p
Is there a browser where this works:
document.forms['signupform'].elements['message'].value

But this doesn't - assuming message isn't a select list or a radio
button[1]?

document.signupform.message.value

I am truly curious this time.

To be perfectly honest, I can't answer that. I don't have enough
experience of the numerous browsers out there (I'm limited to Opera and,
previously, IE**).

Whether there is, or there isn't, this syntax has a couple of advantages:

1) If you decided to use operators or spaces in your attribute values, you
wouldn't have to switch just for that special case.
2) [Related to (1)] It's a consistent syntax, because it works with or
without special characters.
3) It has been mentioned in other threads that that syntax conforms to
DOM, so it should be guaranteed for the foreseeable future.
4) If there are browsers that don't accept the simple, short dot notation,
your code will work fine for them.

The only argument against it is the size, but I would think that is only a
problem for the developer in that it's somewhat cumbersome, not that it
adds too many bytes to the filesize. With sensible use of references, the
impact of the verbosity is negated anyway, so all you're left with is
overhead.

Mike


* Just kidding! :D
** I sometimes use Konqueror, but I stay away from Mozilla and Netscape -
I don't like the interface :) I'll be giving FireBird a go though when it
reaches the Release state.
 
L

Lasse Reichstein Nielsen

Is there a browser where this works:
document.forms['signupform'].elements['message'].value

But this doesn't - assuming message isn't a select list or a radio button[1]?

document.signupform.message.value

Mozilla.
With shorter names:
<form id="foo"><input name="bar" value="baz"></form>
and
document.foo.bar.value
Mozilla gives:
TypeError: document.foo has no properties
while
document.forms['foo'].elements['bar'].value
gives "baz" as it should.

If you had name="foo" in the form, then document.foo would work,
but that's another story.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top