TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-29-2008, 09:37 PM   #1 (permalink)
The Contributor
 
tego10122's Avatar
 
Join Date: Sep 2008
Location: Miami
Posts: 39
Thanks: 7
tego10122 is on a distinguished road
Default Insert Into Text Area

I am making a php comment bbcode script , and I need <input type="button" name="underline" value="u">
to insert <u> </u> into <textarea name="comment"> </textarea>
Send a message via MSN to tego10122
tego10122 is offline  
Reply With Quote
Old 09-29-2008, 11:11 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

this might work

Code:
var element=document.getElementsByName('underline')[0];

element.onclick(function(){
    document.getElementsByName('comment')[0].innerHTML+='<u> </i>';
});
I am a newbie in javascript so I am not sure if it works
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
tego10122 (09-30-2008)
Old 09-30-2008, 10:56 AM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Yes that would work, although there is one thing: the 'onclick' is a property that accepts a reference to a callback, therefore using it as a function will ultimatly end in an error:
Code:
this : element.onclick(function(){

produces this error:
getElementsByName("underline")[0].onclick is not a function
The correct code would be:
javascript Code:
var element=document.getElementsByName('underline')[0];

element.onclick = function(){
    document.getElementsByName('comment')[0].innerHTML+='<u> </i>';
};
This code can be made shorter:
javascript Code:
document.getElementsByName('underline')[0].onclick=function(){
        document.getElementsByName('comment')[0].innerHTML += "<u></u>"
    };

Or you could use jQuery:
javascript Code:
$('input#underline').click(function(){$('textarea#comment').text("<u></ul>")});
if you give the elements id's
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 10-02-2008 at 06:58 PM. Reason: missed the '+' on the second example
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
tego10122 (09-30-2008)
Old 09-30-2008, 01:10 PM   #4 (permalink)
The Contributor
 
tego10122's Avatar
 
Join Date: Sep 2008
Location: Miami
Posts: 39
Thanks: 7
tego10122 is on a distinguished road
Default

what about if its a php form like below and the input field is $_POST['comment']
Code:
<hr><br>Post a comment<br>
	<form method=\"post\" action=\"members.php?id=$user[username]&comments=post\">
	<div style=\"background-color:grey\"><input type=\"button\" value=\"i\"><input name=\"underline\" onclick=\"var element=document.getElementsByName('underline')[0];

	element.onclick(function(){
    document.getElementsByName('$comment_box')[0].innerHTML+='<u> </u>';
	});\"type=\"button\" value=\"u\"><input type=\"button\" value=\"b\" ><input type=\"button\" value=\"img\"><a href=\"#\" onclick=\"smilie(':)'); return false;\" value=\"link\">Link</a></div>
	<textarea id=\"comment\" name=\"comment\" cols='47' rows='10'></textarea></br>
	<input style=\"align:right\" type=\"submit\" value=\"Post\" name=\"reply\">
Send a message via MSN to tego10122
tego10122 is offline  
Reply With Quote
Old 09-30-2008, 05:58 PM   #5 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

For a better practice it's better if you separate the javascript and css from the html code which in this case is embeded in php. It keeps clear separation for the coder.
and like sketchMedia, onclick is a reference callback so, roughly like this.

Code:
<hr><br>Post a comment<br>
	<form method=\"post\" action=\"members.php?id=$user[username]&comments=post\">
	<div style=\"background-color:grey\">
		<input type=\"button\" value=\"i\">
		<input name=\"underline\" \"type=\"button\" value=\"u\">
		<input type=\"button\" value=\"b\" >
		<input type=\"button\" value=\"img\">
		<a href=\"#\" onclick=\"smilie(':)'); return false;\" value=\"link\">Link</a>
	</div>
	<textarea id=\"comment\" name=\"comment\" cols='47' rows='10'></textarea>
	</br>
	<input style=\"align:right\" type=\"submit\" value=\"Post\" name=\"reply\">
	<script type=\"text/javascript\">
		document.getElementsByName('underline')[0].onclick=function(){
        document.getElementsByName('comment')[0].innerHTML = \"<u></u>\"
    };
	</script>
but yeah jquery is better

thanks for the tip sketchMedia

P.D. it would be good if you create a generic onclick function and a parameter which would have what kind of tag is going to be added to the textarea. that's my .5cents
tony is offline  
Reply With Quote
Old 09-30-2008, 06:58 PM   #6 (permalink)
The Contributor
 
tego10122's Avatar
 
Join Date: Sep 2008
Location: Miami
Posts: 39
Thanks: 7
tego10122 is on a distinguished road
Default

I still don't understand what the Onclick Call command will be for the button.. :)
Send a message via MSN to tego10122
tego10122 is offline  
Reply With Quote
Old 09-30-2008, 07:15 PM   #7 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

in your current for you have a onclick attribute in a input tag, here:

HTML Code:
<input name=\"underline\" onclick=\"
that attribute would make sure that whatever value it has would be run when the element is clicked.

the good practiced is in order to avoid combining scripting with markup we use the onclick event handler in javascript. by just assigning the onclick variable of a element in javascript with code like here

Code:
document.getElementsByName('underline')[0].onclick=function(){
        document.getElementsByName('comment')[0].innerHTML = "<u></u>"
    };
in there we assigned the function(){...} to the onclick event of the element document.getElementsByName('underline')[0] which is the element <input name="underline" />

that is why we use the onclick in javascript instead of using it in the markup
tony is offline  
Reply With Quote
Old 10-01-2008, 03:59 PM   #8 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

No offense, but if you use innerHTML the cursor location isn't saved, and if you don't use += or = innerHTML + "<u></u>" all content will be replaced. This will just anger users.

I would suggest reading this tutorial and adapting it to your needs. This is the same thing I used for my work but I got a bit fancy with it; however, this is a great place to start from.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 10-01-2008, 04:00 PM   #9 (permalink)
The Contributor
 
tego10122's Avatar
 
Join Date: Sep 2008
Location: Miami
Posts: 39
Thanks: 7
tego10122 is on a distinguished road
Default

@RobertK , that tutorial was awesome.
__________________
You're Everyday Graphic Artist
Twitter|GigPark|Linked In
Send a message via MSN to tego10122
tego10122 is offline  
Reply With Quote
Old 10-01-2008, 04:14 PM   #10 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

Glad you liked it, I found it helpful as well. Remember, if you struggle with something in Javascript, check the Mozilla Developer Reference. The Mozilla folks at least indicate what is, and what isn't, cross-browser compatible--and so are a great all-in-one reference for most subjects.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 10-01-2008, 05:21 PM   #11 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

I forgot about the +=, good resource thanks man.
tony is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:10 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design