Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-16-2010, 06:45 PM   PM User | #1
johnrivers
New Coder

 
Join Date: Feb 2010
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
johnrivers is an unknown quantity at this point
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.
johnrivers is offline   Reply With Quote
Old 02-16-2010, 07:16 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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

Last edited by Philip M; 02-16-2010 at 07:20 PM..
Philip M is offline   Reply With Quote
Old 02-16-2010, 08:14 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-16-2010, 11:01 PM   PM User | #4
johnrivers
New Coder

 
Join Date: Feb 2010
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
johnrivers is an unknown quantity at this point
How do I use <script type="text/javascript">document.write(qsvalues["email"]);</script>


in an input statement like: <input name="email" type="hidden" id="email" value="+email+" />
johnrivers is offline   Reply With Quote
Old 02-16-2010, 11:49 PM   PM User | #5
johnrivers
New Coder

 
Join Date: Feb 2010
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
johnrivers is an unknown quantity at this point
I used the following code, but can't seem to get the email to send for the

<script type="text/javascript">document.write('<input name="fname" type="hidden" id="fname" value="qsvalues[+fname+]"/>'); </script>
<script type="text/javascript">document.write('<input name="lname" type="hidden" id="lname" value="qsvalues[+lname+]"/>'); </script>
<script type="text/javascript">document.write('<input name="phone" type="hidden" id="phone" value="qsvalues[+phone+]"/>'); </script>
<script type="text/javascript">document.write('<input name="email" type="hidden" id="email" value="qsvalues[+email+]"/>'); </script>



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.
johnrivers is offline   Reply With Quote
Old 02-16-2010, 11:59 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I would not try to use document.write.

Instead, I'd do this:
Code:
<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.
Old Pedant is offline   Reply With Quote
Old 02-17-2010, 12:10 AM   PM User | #7
johnrivers
New Coder

 
Join Date: Feb 2010
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
johnrivers is an unknown quantity at this point
Great job! Now, one small remaining question:

The email portion, that obviously includes an @ symbol, is not coming through. Is there an easy way to fix that?
johnrivers is offline   Reply With Quote
Old 02-17-2010, 12:12 AM   PM User | #8
johnrivers
New Coder

 
Join Date: Feb 2010
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
johnrivers is an unknown quantity at this point
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.
johnrivers is offline   Reply With Quote
Old 02-17-2010, 12:15 AM   PM User | #9
johnrivers
New Coder

 
Join Date: Feb 2010
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
johnrivers is an unknown quantity at this point
Now more info:

I took out the @ and it still wasn't included, and I tried putting an & at the end of the string, but still didn't get anything...
johnrivers is offline   Reply With Quote
Old 02-17-2010, 12:20 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Probably means that the querystring wasn't propery encoded.

The @ symbol is one of many that need to be URL encoded.

Dunno what is generating your querystring, but it's got a bug, methinks.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 02-17-2010, 12:24 AM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I just tried my test page using
Code:
test.html?email=joe%40bar.com
and that worked properly.

Now, to be fair, it also worked using
Code:
test.html?email=joe@bar.com
but now we are into browser dependencies.

Both of those worked properly in both MSIE 7 and FireFox 3.5
__________________
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.

Last edited by Old Pedant; 02-17-2010 at 12:37 AM..
Old Pedant is offline   Reply With Quote
Old 02-17-2010, 12:28 AM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by johnrivers View Post
Now more info:

I took out the @ and it still wasn't included, and I tried putting an & at the end of the string, but still didn't get anything...
So show the actual query string that isn't working???
__________________
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.
Old Pedant is offline   Reply With Quote
Old 02-17-2010, 12:39 AM   PM User | #13
johnrivers
New Coder

 
Join Date: Feb 2010
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
johnrivers is an unknown quantity at this point
Ok, so I am putting the following as my URL

test.html?fname="John"&lname="Doe"&phone="5559991111"&email="fred@aol.com"

And I am receiving:

------------------
fname: "John"

lname: "Doe"

phone: "5559991111"

submit: Submit

------------------


The from address is fred@aol.com

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....
johnrivers is offline   Reply With Quote
Old 02-17-2010, 12:42 AM   PM User | #14
johnrivers
New Coder

 
Join Date: Feb 2010
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
johnrivers is an unknown quantity at this point
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....
johnrivers is offline   Reply With Quote
Old 02-17-2010, 01:05 AM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You are *not* supposed to have quote marks in there.

I'm surprised it works at all with them there.

Hmmm...

But I'm guessing that's not the problem with the email address.

Would you double check that the name in your <form> really matches the name in the querystring???
__________________
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
alert, document.write, form, url variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:06 PM.


Advertisement
Log in to turn off these ads.