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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pupulating form text box with defined value

Status
Not open for further replies.

DrDan1

Technical User
Oct 2, 2002
127
GB
Hi

I'm trying to find a way to populate a text box in a form from a value in a link.

For example, I click on '
This will take me to my form called form.htm.

If I have a form text box, am I able to put that 123 into it as a default value? I'm assuming this would need Javascript and wouldn't be possible with just HTML... if it is all possible to in an html page in the first place, without using API, PERL.. etc.

If anyone can help with this, it would be much appreciated.

Thanks,
Dan



----------------------------------------
Knowing is not enough, we must apply. Willing is not enough, we must do.
--So said the ever wise Bruce Lee
Memorize and live by it!
 
Yes it's possible.
Code:
<html>
<head>

<script type="text/javascript">
<!--//
function getParams() {
	var index = document.URL.indexOf('?');
	var params = new Array();
	if (index != -1) {
		var pairs = document.URL.substring(index + 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();
//-->
</script>

<script type="text/javascript">
<!--//
var passed = unescape(params["text"]);
doument.forms[0].text.value = passed;
//-->
</script>

</head>
<body>

<form>
<input type="text" name="text">
</form>

</body>
</html>

M. Brooks
 
Thanks for that. I tried but it didn't work :/ It's the value I need to change tho so the form should be:

<input type="text" name="name" value="xyz">

Where xyz is the text that I want to get from the link.

So the link will show:

So in this case the value of xyz will show up in the form box as 123.

I tried doing pretty much the same thing you showed me but with the value, but couldn't get it to work :(

Many thanks for this.

----------------------------------------
Knowing is not enough, we must apply. Willing is not enough, we must do.
--So said the ever wise Bruce Lee
Memorize and live by it!
 
FYI: that should be &xyz=123 in the link. Seems to have changed it to show &amp;

----------------------------------------
Knowing is not enough, we must apply. Willing is not enough, we must do.
--So said the ever wise Bruce Lee
Memorize and live by it!
 
This works. I tailored the script to work as you describe above.

link.html
Code:
<html>
<head></head>
<body>

<a href="test.html?xyz=123">Click Here</a>

</body>
</html>
test.html
Code:
<html>
<head>

<script type="text/javascript">
<!--//
function getParams() {
    var index = document.URL.indexOf('?');
    var params = new Array();
    if (index != -1) {
        var pairs = document.URL.substring(index + 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();
//-->
</script>

</head>
<body>

<form>
<input type="text" name="name">
</form>

<!-- note the position -->

<script type="text/javascript">
<!--//
var passed = unescape(params["xyz"]);
document.forms[0].name.value = passed;
//-->
</script>

M. Brooks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top