PDA

View Full Version : document.write help



Rachele7
03-10-2009, 03:44 PM
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.

jscheuer1
03-10-2009, 03:51 PM
Just skip:


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.

Rachele7
03-10-2009, 04:18 PM
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?)

jscheuer1
03-10-2009, 04:37 PM
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:


<input type="submit" value="validate" onclick="return validate(this.form)" />


Change it slightly and make it the form's onsubmit event:


<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:


<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:


document.write (form1.q1.value)

!!

Rachele7
03-10-2009, 04:54 PM
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?

jscheuer1
03-10-2009, 07:15 PM
Get rid of the onclick event here:



<input type="submit" value="validate" onclick="return validate(this.form)" />

Change it slightly and make it the form's onsubmit event:



<form id="form1" name="form1" class="bookingformtext" onsubmit="validate(this);return false;">