Need help using URL Variables to write a form field
Using the url test.html?a=john&b=doe
I am using the following code:
<script type="text/javascript" language="javascript">
function getUrlVars() {
var map = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi;
function(m,key,value) { map[key] = value; }); return map;
}
var john=getUrlVars(map[a]);
alert (getUrlVars(map[a]))
alert (john)
</script>
I can't seem to get an alert to show me anything. The eventual result is not an alert, but a document.write to post the url contents to various form fields for firstname= and lastname= etc.
I am really having a hard time using the document.write function, and am not sure how to employ it, whether in the same script statement or in a separate script in the body.
I am afraid that I do not understand your code. alert (getUrlVars(map[a])) attempts to pass a variable to the function getUrlVars() { but the array map is only created by that function.
document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.
It is your responsibility to die() if necessary….. - PHP Manual
No idea what that JS is supposed to do. Agree w/ Philip that it's just strange.
Here's a straightforward implementation:
Code:
<html>
<head>
<script type="text/javascript">
var qs = location.search.substring(1);
var pairs = qs.split(/\&/g);
var qsvalues = [];
for ( var p = 0; p < pairs.length; ++p )
{
var temp = pairs[p].split("=");
qsvalues[temp[0]] = unescape( temp[1] );
}
</script>
</head>
<body>
Your name is <script type="text/javascript">document.write(qsvalues["name"]);</script>
<br/><br/>
Your age is <script type="text/javascript">document.write(qsvalues["age"]);</script>
</body>
</html>
That's expecting a URL of the form
Code:
pagename.html?name=bob&age=37
or similar.
You could refine it a bit to use a function to get the query string values, so that if no value was passed it comes back with a blank string instead of "undefined". But that's detail work.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
I am using FormMail cgi to send an email, and when I remove the lines above, the email sends with no data, but when I put the code in from above, the email doesn't send.
<html>
<head>
<script type="text/javascript">
function getQSinfo( )
{
var form = document.theForm; // or whatever the name of your form is
var qs = location.search.substring(1);
var pairs = qs.split(/\&/g);
for ( var p = 0; p < pairs.length; ++p )
{
var temp = pairs[p].split("=");
var fname = temp[0];
var val = unescape( temp[1] );
if ( form.elements[fname] != null ) form.elements[fname].value = val;
}
// and I guess you want to submit it, too?
form.submit();
}
</script>
</head>
<body onload="getQSinfo()">
<form name="theForm" ...>
<input name="fname" type="hidden" />
<input name="lname" type="hidden" />
<input name="phone" type="hidden" />
<input name="email" type="hidden" />
</body>
</html>
Untested, but it's simple enough it should work.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
To be more specific, the fname, lname, and phone are all coming through on the submitted email, but the last variable is not included in the email, and the variable name is not shown, so that part looks like it is truncated.
So I guess that now it is working? I would really like the email to be a field in the text, not the in the from address on the email, but that probably isn't a javascript thing....
Also, I would like to use this form script on multiple forms, so have set the form names to be form1, document.theForm1 and document.theForm2, etc. and in the script, i have change it to be:
<script type="text/javascript">
function getQSinfo( )
{
var form = document.theForm1; // or whatever the name of your form is
var qs = location.search.substring(1);
var pairs = qs.split(/\&/g);
for ( var p = 0; p < pairs.length; ++p )
{
var temp = pairs[p].split("=");
var fname = temp[0];
var val = unescape( temp[1] );
if ( form.elements[fname] != null ) form.elements[fname].value = val;
}
// and I guess you want to submit it, too?
// form.submit();
}
{
var form = document.theForm2; // or whatever the name of your form is
var qs = location.search.substring(1);
var pairs = qs.split(/\&/g);
for ( var p = 0; p < pairs.length; ++p )
{
var temp = pairs[p].split("=");
var fname = temp[0];
var val = unescape( temp[1] );
if ( form.elements[fname] != null ) form.elements[fname].value = val;
}
// and I guess you want to submit it, too?
// form.submit();
}
etc.
Is this correct?
My emails from the first script are fine, but the rest the emails come through blank....