Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing querystring vars to another page to populate textboxs javascri

Status
Not open for further replies.

corychauvin

Programmer
Apr 27, 2007
6
CA
Hello,

I need to be able to hard-code a url link that goes to a form page which populates 2 text box's in the form.'

Link would be something like:

this would go to a Quote page with a form and we need the Make and Model textbox's to be filled in from the values of the querystring.

I can get the receiving page to write the values to the page but i can't get the values into the textbox's???. Normally, i use php but for this particular project was done using .shtml / .cgi and my javascript is basic at best.


Here is what i'm currently using, maybe someone will be able to see what i'm doing wrong.
---------------------------------------------

<script language="javascript" type="text/javascript">
function obtainValuesPassed()
{
var infoPassed = location.search;
infoPassed = infoPassed.replace(/\+/g," ");
infoPassed = unescape(infoPassed);
infoPassed = infoPassed.replace(/\?/,"");
var dataPairArray = infoPassed.split(/&|=/);
var extractedData = new Array();
for (var arrayIndex = 0; arrayIndex < dataPairArray.length; arrayIndex++)
{
extractedData[dataPairArray[arrayIndex]] = dataPairArray[++arrayIndex]
}
return extractedData;
}
var valuesPassed = obtainValuesPassed();
if(valuesPassed.length > 0 ){
document.form1[0].Make.value = valuesPassed;
}
</script>
<body onload="obtainValuesPassed()">


<form action="" method="get" name="form1">

<input name="Make" type="text" id="Make"/>
<input name="Model" type="text" id="Model" />
</form>
</body>
 
Assuming your code is correctly retrievning the values (which you indicate it is), then it's probably how you're accessing your form elements which is incorrect.

Looking at this line, I can see one thing immediately wrong with it:

Code:
document.form1[!][0][/!].Make.value = valuesPassed;

Since you only have one form named "form1", there's no need to access it like there were more than one. I'm also assuming that "valuesPassed" is an array, and therefore yo do need to put an index on this to access a specific item. This should suffice:

Code:
document.forms['form1'].elements['Make'].value = valuesPassed[0];

However, you are always assuming that the "Make" variable will be the first one passed - which it may not always be. You might find it better to parse the URL looking for specific tokens rather than assuming their order.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks for taking the time to shed some light on this, but i still can't get this to work??, i don't understand cuz the values will write to the page perfectly using document.write(valuesPassed["Model"]); everything gets stripped out and will write just the values i need for each variable but it won't add them to the textfields? below is the appended code with your suggestion

any more suggestions would be greatly appreciated.
------------------------------------------------------

</head>

<script language="javascript" type="text/javascript">
function obtainValuesPassed()
{
var infoPassed = location.search;
infoPassed = infoPassed.replace(/\+/g," ");
infoPassed = unescape(infoPassed);
infoPassed = infoPassed.replace(/\?/,"");
var dataPairArray = infoPassed.split(/&|=/);
var extractedData = new Array();
for (var arrayIndex = 0; arrayIndex < dataPairArray.length; arrayIndex++)
{
extractedData[dataPairArray[arrayIndex]] = dataPairArray[++arrayIndex]
}
return extractedData;
}

var valuesPassed = obtainValuesPassed();

document.write(valuesPassed["Make"] + "<br>");
document.write(valuesPassed["Model"]);
document.forms['form1'].elements['Make'].value = valuesPassed[0];
document.forms['form1'].elements['Model'].value = valuesPassed[1];
</script>


<body onload="obtainValuesPassed()">

<form action="" method="get" name="form1">
<input name="Make" type="text" id="Make"/>
<input name="Model" type="text" id="Model" />
</form>
</body>
</html>
 
Given that you know that this works:

Code:
document.write(valuesPassed["Make"] + "<br>");
document.write(valuesPassed["Model"]);

Then why not try assigning those values to the text fields instead?

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Isn't that what these lines are supposed to do?

document.forms['form1'].elements['Make'].value = valuesPassed[0];
document.forms['form1'].elements['Model'].value = valuesPassed[1];

I'm not sure i'm understanding you correctly?
 
So you take the parts of your script that you know contain the values you want:

Code:
document.write([!]valuesPassed["Make"][/!] + "<br>");
document.write([!]valuesPassed["Model"][/!]);

and put them into the form fields using the left-hand sides of the assignments in my script:

Code:
[!]document.forms['form1'].elements['Make'].value =[/!] valuesPassed[0];

(and so on, for 'Model').

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan,
I really appreciate all your help, so what your saying is to actually "Bind" them into form fields? how would i do that?

I know in php i would do something like this:
<input name="textfield" type="text" value="<?php echo $_GET['Make']; ?>" />

What would be the equivalent in javascript?

are you saying to do something like:
<input name="textfield" type="text" value="document.forms['form1'].elements['Make'].value" />

I know this is wrong cuz i tried it... i missing some syntax or something?

Again i appreciate all your time and effort.

 
No "binding" necessary. Simply assign the values (the first parts highlighted in red in my above post) to the correct form fields (the second parts highlighted in red in my post above).

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
yes thanks, i did manage to get this working, but i eneded up taking a completely different approach, i found a .js library file that did exactly what i needed it to with only a little code i had to write.

Thanks again for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top