SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with OnClick in form

    First, I now 0 about JavaScript I have just begun learning PHP, so I have my hands full with that, but I need to use a little JS in my scripts, so I'm looking for some help.

    I'm doing a news script and wants people to be able to quickly add a link or some other text in the <textarea> by clicking on a link next to the box. In other words, when the user clicks a link, some text is added right away to the text box (<textarea>). (Like when you click on a smilie in these forums, to have them inserted.)

    Can someone write and example from start to finish, that makes it possible to add just the line TEST? Then from that, I can figure out how to add additional links. (I'm not asking you to write a whole form, just the JavaScript part

    I don't know if you need this information, but my text box is simply called:

    PHP Code:
    <textarea name="newstext" rows="15" cols="60</textarea> 
    Can anyone help?

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know the exact script you want exists at:

    http://javascript.internet.com/forms/ but I couldn't find it.. so hunt around.

    Any of these might be of use to you:

    http://javascript.internet.com/forms/items-list.html
    http://javascript.internet.com/forms...organizer.html
    http://javascript.internet.com/forms/memory-cells.html
    http://javascript.internet.com/forms/copy-name.html

    or at the very least they'll giev you an idea where to start.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to learn Javascript first. Although someone might say that you don't need Javascript, it is the next step in your programming studies. I have an excellent Javascript tutorial at my website, HTML4ME.COM that you can learn from.

    Taking on PHP without knowing Javascript will most likely get you spun around totally confused after a while. Trust me, Javascript will make PHP so much easier to learn.

    http://www.html4me.com/

    Learn Javascript first before continuing on with PHP.

    Oh, and about your script, it is very simple to do what you want to do. You could write the script yourself after doing my JS tutorials on my website.

    Here is the script:

    <Script language=Javascript>
    function writeInText()
    {
    window.document.form1.textarea1.value = 'TEST?';
    }
    </script>

    <form name="form1">
    <textarea name="textarea1" rows="2" cols="35"></textarea>
    </form>

    <a href="javascript:writeInText()";>Write in 'TEST?' into the textarea box above</a>

    That is all. It is all very simple. You can learn that and so much more, and JS is almost a must before PHP even though some think not. PHP just goes so much faster after knowing Javascript because the syntax is so similar that PHP is easy to grasp. And no, it does not work the same way to learn PHP first and Javascript second. JS first, PHP second.

    BTW, in the code above, there is no space between java and script. One word for the link to put in the text 'TEST?' into the textarea box.
    Last edited by gregc; Feb 22, 2002 at 12:39.

  4. #4
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    Well your code doesn't work, and I disagree about Javascript before PHP.

    I don't know how to write javascript, but I know how to error check it since I know PHP. If you want to learn PHP then learn C then learn PHP, and then Javascript.

    And in my experience knowing PHP is going to help you out more than knowing Javascript, esp since pretty much any Javascript you could ever want has been written.

    Put it this way, if you have the time, learn both, but I don't think there is a "best" order.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  5. #5
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    weirdbeardmt, the reason why I say this is, if you learn Javascript well first, by the time you move onto PHP, your syntax writing is developed a bit and the type of coding style that PHP has will not be too hard to understand. Also, Javascript is much more simpler than PHP, and does not have anything to do with server side, making it a little bit easier to understand.

    It is the next grade up after HTML and CSS, while PHP I think should be done after Javascript. JMO.

    BTW, for some reason this board puts a space between java and script in the link. When using the script, you need to take the space away from there.

    Here is a link of the script in action:

    http://www.html4me.com/stuff/test.html

    See? It works....

  6. #6
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might find this hard to believe but I think PHP is much easier than Javascript. But each to his own

    And your code does not work (I knew about the space error, damned vB ) -- even the link on your site doesn't work -- you still have the space. And when you take the space out you get "Object does not support this method".
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  7. #7
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I see that you have edited your code from your original:

    Code:
    window.document.form1.textarea1.value("TEST?");
    to

    Code:
    window.document.form1.textarea1.value = 'TEST?';
    Very cheeky of you. But it does work.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  8. #8
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol! I forgot to take the space out myself. Well, now you can try the link:

    http://www.html4me.com/stuff/test.html

    I tested it in IE 6 and in NS 6 and they both work (for PC - I didn't try Mac, but it should work on Mac too).

    What browser and OS are you using, weirdbeard? This javascript is so basic, it is hard to believe that it doesn't work for you.

  9. #9
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does work now -- you changed your code... read my above post!
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  10. #10
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, we posted near to the same time. yeah, it does work now.

  11. #11
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for posting a link to your tutorial and thank you all for your input. I appreciate it.

    I have tried your test script (Again, thanks for writing the code) the problem is, that you can't write any thing else. I would like it so you can write some stuff, click the link, and continue writing after TEST has been inserted. As it is now, clicking the link deletes everything from the box and then puts in TEST.

    I will look at your tutorial tomorrow (it's late here now) and then I may be able to figure it out myself

  12. #12
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to append a string (e.g. 'TEST'), then just change it to:

    window.document.form1.textarea1.value += 'TEST?';

    (note the '+')


    M@rco

  13. #13
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, sorry about that. I noticed that myself.

    Here is a script I forgot about, which actually did this:

    <SCRIPT language=Javascript>
    function jsin(element) {
    document.form1.textarea1.value = document.form1.textarea1.value + "" + element + "\n"; }
    </SCRIPT>
    </head>
    <body>
    <form name="form1">
    <textarea name="textarea1" rows="3" cols="35"></textarea>
    <BR>
    <a href="javascript:jsin('TEST?');">Insert 'TEST?' into textarea box</a>
    </form>
    </body>
    </html>

    This also creates a line break for each time you insert text. If you don't want this, simply take out the \n in the JS code (don't take out the quotes though). I just tested the above code, and it works fine.

    You can see the whole script that I wrote a while ago here:

    http://www.html4me.com/a_javascript.html

    Sorry about this...

    GregC

  14. #14
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using += is a little more efficient...!


    M@rco

  15. #15
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    I tried to do the script myself, but couldn't get it to work, so I'll just use the last script gregc wrote. It works just fine

    Just one more question, what do I do if I want a text box, where people can enter something and then it is added along with some more text?

    Example:

    User writes in textbox: www.yahoo.com and presses the submit button, the script adds it to the textarea with <a href="http:// and </a> around it, like so:

    <a href="http://www.yahoo.com</a>

    I promise I won't ask for anymore now
    Last edited by Dalixam; Feb 26, 2002 at 13:05.

  16. #16
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, PHP was easier for me to do: (this might help you out a bit too, even though you should be learning JS right now)

    First of all, here is the link:

    (very basic script)

    http://www.html4me.com/php_tests/go2site.php

    Here is the code for that page:

    <?php

    echo "<form name='form1' action=send2page.php>";

    echo "<input type='text' name='url' value='http://'>";
    echo "<BR><BR>";
    echo "<input type='submit' value='Submit'>
    </form>";
    ?>

    And the page it directs to in the form:

    <?php

    $url = StripSlashes($url);

    $data = "<a href=\"$url\">$url</a>";

    echo "<textarea rows='2' cols='70'>$data</textarea><BR>";

    echo "<BR><BR><a href=go2site.php>Go back to first page</a>";

    if($url == "")

    {

    echo "<font face=Arial size=2>You did not enter any data into the form.
    Please click the back button and submit again.</font>";

    }

    ?>

    I didn't have much time to do this-- enjoy!

    GregC


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •