...

View Full Version : Pass parameters to HTML form via URL



getsun
01-11-2007, 07:50 AM
I am a newbie. I want to pass two parameters to an HTML form on my site, like this:-

http://usbdial.com/userinfo.htm?email=test@test.com&cid=1234


This is what I've done so far:

Created a simple HTML form.
Added some Javascript, using script found on the internet.
Ran the form at
http://usbdial.com/userinfo.htm?email=test@test.com&cid=1234

It does not pass the parameters - it shows that the fields are undefined.

Can anyone see what I've done wrong?

Also, when you view the page source, function SymError() appears twice, even although in my source it appears only once. Any idea why it appears twice?

Any guidance will be welcome.

TripperTreats
01-11-2007, 08:53 AM
Your only problem is this:


pvName = unescape(params["uname"]);
document.myForm.email.value = pvName;

pvName = unescape(params["pass"]);
document.myForm.cid.value = pvName;Just change "uname" to "email" and "pass" to "cid". Also, for the life of me I can't figure out why this isn't causing an error:

function getParams() {
var idx = document.URL.indexOf('?');
var params = new Array();

if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
}
}
return params;
}
params = getParams();"params" is being assigned a value before being declared a variable.
Also, inside the function, "params" should be an object, not an array.
At any rate, I tested it, and you should replace it with this:


function getParams() {
var idx = document.URL.indexOf('?');
var tempParams = new Object();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('=');
tempParams[nameVal[0]] = nameVal[1];
}
}
return tempParams;
}
var params = getParams();

getsun
01-12-2007, 01:09 AM
Thanks TripperTreats. Your suggestions fixed the issue.

Another question if I may:

When I type into the bowser:
http://usbdial.com/userinfo.htm?email=test@test.com&cid=1234
then the fields show correctly with the parameters in the fields.

However typing this:
http://usbdial.com/userinfo.htm
shows the fields as "undefined".

If I don't pass any parameters in the URL, do you know how can I get the fields to be blank rather that filled with "undefined"?

TripperTreats
01-12-2007, 01:35 AM
No problem. Replace your source code with this (I've repeated the whole thing so you can just copy and paste):


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy for Windows (vers 1 June 2005), see www.w3.org">
<meta http-equiv="Content-Language" content="en-au">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>User info</title>
<script language="JavaScript" type="text/javascript">
<!--

function SymError()
{
return true;
}

window.onerror = SymError;

var SymRealWinOpen = window.open;

function SymWinOpen(url, name, attributes)
{
return (new Object());
}

window.open = SymWinOpen;

//-->
</script>
<script language="JavaScript" type="text/javascript">

<!-- Begin
function getParams() {
var idx = document.URL.indexOf('?');
if (idx != -1) {
var tempParams = new Object();
var pairs = document.URL.substring(idx+1,document.URL.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('=');
tempParams[nameVal[0]] = nameVal[1];
}
return tempParams;
}
}
var params = getParams();


// End -->
</script>
</head>
<body>
<form method="post" name="myForm" action="--WEBBOT-SELF--" id="myForm">
<!--webbot bot="SaveResults" U-File="C:\TEST\Tutorial\Emails sent\_private\form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
<p> Email:
<input type="text" name="email" size="20">
</p>
<p> Account:
<input type="text" name="cid" size="20">
</p>
<p>
<input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2">
</p>
</form>
<script language="JavaScript" type="text/javascript">

<!-- Begin
pvName = unescape(params["email"]);
if (pvName) document.myForm.email.value = pvName;

pvName = unescape(params["cid"]);
if (pvName) document.myForm.cid.value = pvName;
// End -->

</script>
<script language="JavaScript" type="text/javascript">
<!--
var SymRealOnLoad;
var SymRealOnUnload;

function SymOnUnload()
{
window.open = SymWinOpen;
if(SymRealOnUnload != null)
SymRealOnUnload();
}

function SymOnLoad()
{
if(SymRealOnLoad != null)
SymRealOnLoad();
window.open = SymRealWinOpen;
SymRealOnUnload = window.onunload;
window.onunload = SymOnUnload;
}

SymRealOnLoad = window.onload;
window.onload = SymOnLoad;

//-->
</script>
</body>
</html>

getsun
01-12-2007, 06:10 AM
Great! Excellent solution.

Thanks Jason.

OXY
07-28-2008, 06:03 AM
Hi,

I found this script very useful and it does exactly what I need, however Im facing the following problem I wonder you could help me with.

My form was generated by a CRM application (SalesForce) and the custom fields are named such as 00N30000002PCO8.

If I use this name in the script, it simply does not work.

I believe it has to do with the name starting with a "0" (zero) or a number since the script works fine if I place the letter "a" at the start of the name such as: a00N30000002PCO8. But then, by changing the name, the field content is not captured by the CRM application.

You can find the script NOT working because of this problem at:

http://www.liveperson.com.br/temp/web_to_lead/demo_request.htm?site=www.site.com.br&nome=OXY&telefone=1234567890&empresa=company&protocolo=LTK123456789X&email=mail@host.com

A really appreciate any help/reply :thumbsup:

Best Regards,
OXY



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum