Results 1 to 7 of 7

 

Thread: How to insert the 'output' from a javascript script into form field

  1. #1
    Registered User

    Status
    Offline
    Join Date
    Aug 2003
    Location
    HUDDERSFIELD, UK
    Posts
    750
    Thanks
    0
    Thanked 0 Times in 0 Posts


    Hi

    I'm not very good at JavaScript and I thought I might as
    well ask my question here as scour Google for several
    hours.

    Basically, I want to insert the output from a JavaScript
    random quote generator into the input of a Perl/CGI
    (hidden) form field.

    Do I need to find a suitable JavaScript random quote
    generator? If so, any ideas? Or can I just modify
    an existing random quote generator to output to
    a hidden form field? If so, how?

    Or should I just be using PHP?

    Many thanks for any ideas you may have.

    Steve

  2. #2
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    323
    Thanks
    1
    Thanked 4 Times in 2 Posts
    Hi Steve,

    It's very simple to set the value of any form element using JavaScript. All you need to do is make sure that your form has a name="" attribute and your hidden field has an id="" attribute. For example:

    Code:
    <form name='myForm'>
      <input type='hidden' name='quote' id='quote' value='' />
    </form>
    Then, assuming that you find a random quote generator that returns a quote from a function called getQuote(), you could set the value of the hidden field like this:

    Code:
    <script type='text/javascript'>
      document.myForm.quote.value = getQuote();
    </script>
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!

  3. #3
    Registered User

    Status
    Offline
    Join Date
    Aug 2003
    Location
    HUDDERSFIELD, UK
    Posts
    750
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Dmorisson

    Great timing, as I'd just found a similar solution (without the id=quote) bit.

    So, like everything, it's easy when you know how!

    Would I be right in assuming that a default value could be set
    in cases where no javascript is present by coding the following?

    Code:
    <form name='myForm'>
      <input type='hidden' name='quote' id='quote' value='Default quotation goes here' />
    </form>
    Once again, thanks

    Steve

  4. #4
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    323
    Thanks
    1
    Thanked 4 Times in 2 Posts
    Correct - the JavaScript will simply overwrite the default value.
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!

  5. #5
    Registered User

    Status
    Offline
    Join Date
    Aug 2003
    Location
    HUDDERSFIELD, UK
    Posts
    750
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, can't get this to work, sorry.

    Should I be putting this code in the HEAD section (or in the form)?
    <script type='text/javascript'>
    document.myForm.quote.value = getQuote();
    </script>
    and why can't I just add this to the getQuote() routine?
    document.myForm.quote.value = quote;
    Hmm, I clearly know nowt about JavaScript
    Lol

    Thanks in advance
    Steve

  6. #6
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    323
    Thanks
    1
    Thanked 4 Times in 2 Posts
    When writing JavaScript that refers to other elements on the page you should make it last thing on the page, otherwise the items that it refers to will not yet be known by the browser.

    With regards just using:
    Code:
    document.myForm.quote.value = quote;
    ...that's fine if your script has previously stored the quote in a variable called quote. My example used function call that returns the quote instead - the result is the same.

    The following code should work - it will set a quote into the variable quote; set the hidden form field, and then display the value of the hidden form field in an alert box so you can see the contents...

    Code:
    <html>
      <body>
        <form name='myForm'>
          <input type='hidden' name='quote' id='quote' value='' />
        </form>
        <script type='text/javascript'>
          quote = 'This is the quote of the day!';
          document.myForm.quote.value = quote;
          alert(document.myForm.quote.value);
        </script>
      </body>
    </html>
    Hope this helps!
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!

  7. #7
    Registered User

    Status
    Offline
    Join Date
    Aug 2003
    Location
    HUDDERSFIELD, UK
    Posts
    750
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi dmorison

    THANKS.

    Yes, that worked (eventually). I still took 2 hours frigging
    around before I got it working, but that's probably more to
    do with it being the end of my day than anything else.

    Thanks very much.

    I owe you a beer, or something for sure!
    Cheers

    Steve



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Form spam problem
    By mibut in forum Programming
    Replies: 16
    Last Post: 07-02-07, 01:22 AM
  2. Use javascript to insert tracking code.
    By Doghouse in forum Programming
    Replies: 9
    Last Post: 22-02-06, 01:07 PM
  3. Javascript enabled Browser check script required
    By DanielCoe in forum Programming
    Replies: 5
    Last Post: 18-11-05, 05:15 PM
  4. DWMX Form field distortion
    By Shane in forum Programming
    Replies: 9
    Last Post: 06-07-04, 04:28 PM
  5. Script for capturing HTML from Javascript
    By Reddy in forum Programming
    Replies: 0
    Last Post: 23-09-03, 01:28 AM

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
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2