...

View Full Version : Passing value taken from a form field to another form thru a hyperlink.



NewbieNeedsHelp
08-26-2002, 06:12 AM
Hi to all,

:o A newbie here..

How to get the value (ie. from a drop down list called 'select') and pass it thru a hyperlink to another page?

Thanks in advance!

glenngv
08-26-2002, 07:05 AM
<a href="#" onclick="location.href='page2.htm?'+document.yourFormName.select.options[document.yourFormName.selectedIndex].value">Page 2</a>


then in your page 2: (assuming you have only one parameter passed in the URL)

if (location.search.length>1)
alert(location.search.substring(1))

joh6nn
08-26-2002, 07:19 AM
<A HREF="thenextpage.htm" onclick="theFunction(); return false;">click click</A>

<script>
function theFunction() {
var selected = document.theForm.theSelectBox.options[document.theForm.theSelectBox.selectedIndex].value;
window.location = "thenextpage?" + selected;
}
</script>


that will pass the value to the next page. it does this first finding the selected value. then, it adds the selected value to the end of the next page's URL, using the question mark (?).

on the next page, you use the following to retrieve the value:

<script>
if ( window.location.search ) {
var value = window.location.search.slice(1);
}
</script>

what that does, is take the url, look for the search string ( which is the ?, and everything after it ). if the search string exists, then it strips it of the ?, and assigns what's left to the variable value.

NewbieNeedsHelp
08-26-2002, 08:14 AM
Thanks to the above 2 replies..Actually i intend to let the customer choose the color of the prod they intend to buy, thus the purpose of the list box.


I tried this:

<a href="http://www.sitename.com/cf/add.cfm?userid=1111111&product=12+roses+document.Prod1.select.options[document.Prod1.selectedIndex]&price=70.00">Add to Cart</a>

But after passing the values to the 2nd page(payment gateway), what was displayed is:

"12 roses document.Prod1.select.options[document.Prod1.selectedIndex].value"

How do I let it display:
"12 roses Red"

glenngv
08-26-2002, 08:33 AM
<a href="#" onclick="location.href='http://www.sitename.com/cf/add.cfm?userid=1111111&price=70.00&product=12%20roses%20'+document.Prod1.select.options[document.Prod1.selectedIndex].value">Add to Cart</a>

i supposed you have the server-side code handle the retrieval of the query string, so location.search is not needed.

joh6nn
08-26-2002, 08:43 AM
assign the value to a variable, and then pass the variable. eg,

<A HREF="thenextpage.htm" onclick="theFunction(); return false;">click click</A>

<script>
function theFunction() {
var selected = document.theForm.theSelectBox.options[document.theForm.theSelectBox.selectedIndex].value;
window.location = "thenextpage?" + selected;
}
</script>

by trying to pass this:
document.theForm.theSelectBox.options[document.theForm.theSelectBox.selectedIndex].value
you're passing a reference, that won't exist on the next page. the form is on this page, when the next page loads, how is it going to reference that form? that's why you pull the value out of that reference, and then build the value into the url.

also don't write your link like this:
<A HREF="#" onClick="....">

this is a very common, very popular form, but there are good reasons not to do that. suppose your user has javascript turned off. if they click on that link, they'll be brought to the top of the page. if they had to scroll down at all in order to get to that link, they will be ticked off

<EDIT>
or what glenn said. whatever.
</EDIT>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum