Forum Moderators: open

Message Too Old, No Replies

Grab Data from URL and Insert into a Form

         

matthewamzn

7:19 pm on Feb 16, 2007 (gmt 0)

10+ Year Member



I have a URL that contains variables that I want to put into a form on a regular html page. I want to do this so that customers don't have to fill out a product's details they have a question about. Is there a javascript or something I could use? I know how to do it with php, but the page is just regular html.

The URL would contain something like this:
?product=toy&price=12&color=blue

I want to grab this data to prefill parts of a form. (hope that makes sense)

penders

1:26 pm on Feb 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As far as I know in JavaScript there is no 'automatic' way of reading the variables from the URL as there is in PHP. But you can analyse the
location.href
string which contains all the info and break it down to get what you want.

In brief... find where the querystring starts with .indexOf('?') and remove the part of the querystring past the # (if present). split('&') the querystring into an array of key/value pairs, then I guess you can split('=') it further to ultimately get an associative array in the form: param['key'] = 'value' etc...?