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

Putting a user input in a textarea onload

P: n/a
Hey
Im making a homepage in php. I use a html form to put data into mysql
and i want to make some buttons which inserts user input values into a
textarea. I have used a button like this:

<input type="button" value="Add Quote"
onclick="document.getElementById('nyhed').value+=''">

The button works fine and insterts into the textarea.
Although I want to make a button which onclick asks the user for a
input and inserts it into the textarea. The script would in this case
ask for an input and insert
USER INPUT
into the
textarea.

Does someone know how to do that?

Apr 24 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
da****************@gmail.com said on 24/04/2006 5:06 PM AEST:
Hey
Im making a homepage in php. I use a html form to put data into mysql
and i want to make some buttons which inserts user input values into a
textarea. I have used a button like this:

<input type="button" value="Add Quote"
onclick="document.getElementById('nyhed').value+=''">

The button works fine and insterts into the textarea.
Although I want to make a button which onclick asks the user for a
input and inserts it into the textarea. The script would in this case
ask for an input and insert
USER INPUT
into the
textarea.


Why not let the user enter whatever into the textarea and add the quote
tags when you get the response at the server?

You can use a prompt to get user input, but it's rather kludgy to use a
dialog to get input that is dropped into a page anyway (I hate such things).

<textarea onclick="
this.value = '[quote]'
+ prompt('Enter some text')
+ '[quote]';
"></textarea>
But no recommended.
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 24 '06 #2

P: n/a
RobG wrote:
da****************@gmail.com said on 24/04/2006 5:06 PM AEST:
Im making a homepage in php. I use a html form to put data into mysql
and i want to make some buttons which inserts user input values into a ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ textarea. I have used a button like this:

<input type="button" value="Add Quote"
onclick="document.getElementById('nyhed').value+=''"> ^^^^^^^^^^^^^^^ The button works fine and insterts into the textarea.
Although I want to make a button which onclick asks the user for a
input and inserts it into the textarea. The script would in this case
ask for an input and insert
USER INPUT
into the
textarea.
Why not let the user enter whatever into the textarea and add the quote
tags when you get the response at the server?


I think you miss the point :)

Obviously, this is for some kind of bulletin board or Wiki, where
the "
...
" markup is to indicate text quoted from the
precursor or a third source. (Have you never used such before? ;-))
You can use a prompt to get user input, but it's rather kludgy to use a
dialog to get input that is dropped into a page anyway (I hate such
things).

<textarea onclick="
this.value = '[quote]'
+ prompt('Enter some text')
+ '[quote]';
"></textarea>
Since prompt() returns `false' if the user cancels, one wants to enhance
this a bit at least (watch for word-wrap):

<textarea
onclick="var s; this.value = '[quote]' + ((s = prompt('Enter some
text')) ? s : "") + '[quote]';"</textarea> But no recommended.


True. Newline in attribute values is not recommended. And one should use
a button instead, as the OP already did (only his referencing was
unnecessarily complicated and error-prone). That way the button could be
generated by script (so that without script, users do not have to worry
about a dysfunctional button) and could facilitate for inserting the markup
at the cursor position, or replace the current selection. Quick hack for
Geckos:

<meta http-equiv="Content-Script-Type" content="text/javascript">
...
<form ...>
...
<script type="text/javascript">
function addQuote(o)
{
// if nothing is selected, selStart == selEnd
var
selStart = o.selectionStart,
selEnd = o.selectionEnd,
s;

if ((s = window.prompt('Enter some text'))
{
oSel.value = oSel.value.substring(0, selStart)
+ '
' + s + '
'
+ oSel.value.substr(selEnd);
}
}

document.write('<input type="button"'
+ ' value="Add Quote"'
+ ' onclick="addQuote(this.form.elements[\'nyhed\']);">');
</script>

<textarea name="nyhed" ...></textarea>

...
</form>

See <URL:http://faqts.com/knowledge_base/view.phtml/aid/17749/fid/53> (or
Wikimedia etc. source code) for (a hint towards) a cross-browser approach.
PointedEars
Apr 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.