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

How to write URL passed value into Form textarea

Status
Not open for further replies.

MVD100

Technical User
Dec 10, 2008
49
US
I have a link //pages/form.htm?num=1 that opens a form page. I want to read the passed variable num=1 into the form textarea to read "You have requested document 1" where the value 1 comes from the URL passed value in num. I will only be passing one variable at this time. So far my form looks like this:
<td colspan='3'><textarea name='Comments' cols='45' rows='10'>You have requested document </textarea></td>
How do I get the value 1 from the URL into the textarea?

Thanks in advance for your response.
 
you want to do this in Javascript?

1. Get the url into a variable
2. Split the variable by the question mark
3. Split each element of the array by the equal sign
4. Get the 2nd value of the 2nd split and depending on what the first value of the 2nd split is using the DOM to populate the the text area.

Do all of this using the onload event.


--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Why not simply split on the equals sign and read the second value? It would remove the need for a loop and subsequent split operations.

Dan





Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
BillyRayPreachersSon said:
Why not simply split on the equals sign and read the second value? It would remove the need for a loop and subsequent split operations.

For this instance, yes that would work, but if you have more than 1 value then you'll need multiple splits - no???

I guess you would need to split it again by the ampersand:

Code:
mypage.html?fname=john&lname=smith
<input type="text" name="fname" id="fname" value = "" />
<input type="text" name="lname" id="lname" value = "" />

so the first split on the ? would give you:
ra1[0]: mypage.html
ra1[1]: fname=john&lname=smith

splitting ra1[1] on the & would give you:
ra2[0]: fname=john
ra2[1]: lname=smith

splitting each of ra2 on the = would give you
ra3[0] = fname,lname
ra3[1] = john,smith
^ yeah, it's not exactly that, but you get the point.

So you could say in js

Code:
for (var i = 0 ; i < ra2.length; i++) {
  var ra3 = ra2[i].split('=');
	if ($(ra3[0]) {
		$(ra3[0]).value = ra3[1];
	}
 }

You're more experienced at JS than I am, Dan - but that is the only way I would know how to do it - if you have other suggestions, please share as they would come in useful for me :)

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
I tried this:
<script>
var urlStr = document.search.substring(1).split("=");
alert('Variable is:' + urlStr);
</script>

This does not work. Why not? The alert is only for testing. Ultimately I will need the variable placed in a form textarea. I don't have more than one varaible being passed.

Thanks.
 
Here is an example for doing this.

Code:
<html>
	<head>
		<script type="text/javascript">
			function getDocNumberFromURL()
			{
				var daURL = document.URL;
				if(daURL && daURL.indexOf("?") != -1 && daURL.indexOf("=") != -1) {
					var docNum = daURL.split("=")[1];
					alert(docNum);
				}
			}
		</script>
	</head>
	<body onLoad="javascript:getDocNumberFromURL();">
		
	</body>
</html>

This (again) just alerts, but once the docNum is obtained, you can set it to the textarea.

Hope this is what you are looking for.

Nitin
 
Thanks Nitin, that helps. Now how do I display the variable docNum in the HTML text area? I have:
<textarea name='Comments' cols='45' rows='10'>You selected document number docNum;</textarea>

Thanks
 
Code:
<html>
	<head>
		<script type="text/javascript">
			function getDocNumberFromURL()
			{
				var daURL = document.URL;
				if(daURL && daURL.indexOf("?") != -1 && daURL.indexOf("=") != -1) {
					var docNum = daURL.split("=")[1];
					[COLOR=red]document.getElementById("Comments").value = 'You selected document number '+docNum;[/color]
				}
			}
		</script>
	</head>
	<body onLoad="javascript:getDocNumberFromURL();">
		<textarea [COLOR=red]id="Comments"[/color] name='Comments' cols='45' rows='10'></textarea>
	</body>
</html>

Nitin
 
vicvirk said:
For this instance, yes that would work, but if you have more than 1 value then you'll need multiple splits - no???

Of course, but that's not the URL structure the poster gave us. Sometimes it's better to do the minimum required to achieve the job in hand, rather than what might be in the future :)



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Problem solved. Outstanding!
Thanks Nitin. Your code works perfect.
Thank you everyone for the help. BillyRay is right, sometimes it is better to do the minimum...

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top