Results 1 to 6 of 6

Thread: document.write help

  1. #1
    Join Date
    Mar 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default document.write help

    Newbie here:

    I have a script that claculates a price based on info entered by the user onto a form (the script is called using src"...").
    I then used this to try to insert the calculated price onto a particular field in the form.

    document.form1.q1.value = (price)

    document.write (form1.q1.value)

    The problem is that when the user click the validate button on the form, the price is displayed on a new web page.

    How do i get the price to display in the correct field on the form.


    Thanks for your assistance.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Just skip:

    Code:
    document.write (form1.q1.value)
    Get rid of it. The other directive you have should be all that is required. Also, if document.write() is used after page load, it will do just what you observed - basically wipe out the page with the new content.

    If you want more help:

    Please post a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your reply.

    Here is the link for the webpage in question:

    http://www.surfcelive.com/test/


    On IE the price is displayed on a different page while on Firefox, the price is displayed on in the form but the page refreshes (how do i stop it from doing that?)

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Your form is submitting. In real life you may or may not want that. And in real life, unless you restrict access to the page to users with javascript enabled, the script won't run and the form will submit anyway.

    That said, get rid of the onclick event here:

    Code:
    <input type="submit" value="validate" onclick="return validate(this.form)" />
    Change it slightly and make it the form's onsubmit event:

    Code:
    <form id="form1" name="form1" class="bookingformtext" onsubmit="validate(this);return false;">
    Now, it will never submit as long as javascript is enabled, a good method for testing the rest of the code. If you were to use:

    Code:
    <form id="form1" name="form1" class="bookingformtext" onsubmit="return validate(this);">
    If the function validate() returns false, the form will not submit. Any other return value or no return value, the form will submit. If you want to use this method, look to your validate() function to see what it returns under the various circumstances it may encounter and ensure that the correct value (basically true or false) for your desired result as to whether the form should submit or not is returned by the validate() function under each of these possible circumstances.

    Oh, and get rid of:

    Code:
    document.write (form1.q1.value)
    !!
    Last edited by jscheuer1; 03-10-2009 at 04:42 PM. Reason: add bit about document.write
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the info.


    I dont want the form to submit at all.

    I want it to display the price in the "quote" field from the javascript.

    I removed the document.write bit and it no longer goes to a new webpage. Thanks.


    How do i get the price to remain in the box till the user clicks the clear button?
    Last edited by Rachele7; 03-10-2009 at 05:18 PM. Reason: removing bit about document.write

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Get rid of the onclick event here:


    Code:
    <input type="submit" value="validate" onclick="return validate(this.form)" />
    Change it slightly and make it the form's onsubmit event:


    Code:
    <form id="form1" name="form1" class="bookingformtext" onsubmit="validate(this);return false;">
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •