Google Answers Logo
View Question
 
Q: JavaScript - using query string from url as default value for an html form ( Answered,   1 Comment )
Question  
Subject: JavaScript - using query string from url as default value for an html form
Category: Computers > Programming
Asked by: dswilder-ga
List Price: $21.00
Posted: 28 Oct 2005 20:55 PDT
Expires: 27 Nov 2005 19:55 PST
Question ID: 586278
I would like to use javascript to insert a default value into an html
form from the url.  I know how to submit the form, but need to know how to grab
the value from the url and insert it as a default value and
display it in the form.

this is an example url: www.google.com/index.html?email=gooogle@gmail.com

the value google@gmail.com should show up in the form in the email field
Answer  
Subject: Re: JavaScript - using query string from url as default value for an html form
Answered By: palitoy-ga on 31 Oct 2005 01:22 PST
 
Hello dswilder-ga

Thank-you for your question.

The easiest way to achieve this is to look for the string ?email= in
the URL.  This can be done using "document.URL.indexOf" which returns
the position of the string ?email= in the URL if it exists or -1 if it
does not.

Once we know where the string ?email= exists in the URL we can simply
take the section of the URL we require using "document.URL.substring"
and place this value in the form element we desire.

In summary a form using this plan of action would look something like this:

<form name="test">

<script>
// see if there is a ? in the url
var is_q = document.URL.indexOf('?email=');

// check the ? is there
if (is_q != -1)
{ 
// write out the form box with the information after the ? in the box
document.write("<input type=\"test\" name=\"textbox\"
value='"+document.URL.substring(is_q+7, document.URL.length)+"' />");
}
else {
document.write("<input type=\"test\" name=\"textbox\" value='No email
address given' />");
};

</script>

</form>

To try this out simply paste everything from the <form> to </form>
tags into a blank HTML document.

Hopefully it should be a simple task from here to adapt this to your
page.  Should you require any further information on this subject
please do not hesitate to ask for clarification.
Comments  
Subject: Re: JavaScript - using query string from url as default value for an html form
From: jamal_s-ga on 06 Nov 2005 07:15 PST
 
If you need other solution you can write :P

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy