Write a variable to a form input value

So I have a variable that I want to write as a form input value, if I use

<input name=fid_17 value=<script language="javascript">document.writeln(variable);</script>>

I get “scr” in the input box.

What is the correct way to write in the variable?

You can’t insert a script object into an attribute value like that.

You need to assign the value with a function or something. There are several ways to do it. Here’s one possibility

<script language="javascript" type="text/javascript">
<!--
function initInput()
	{
	var variable = somevalue;
	document.forms[0].fid_17.value = variable;
	}
//-->
</script>
</head>

<body onLoad="initInput()">
	<form>
	<input name="fid_17" id="fid_17" value="">
	</form>
</body>

Oh, and get accustomed to surrounding your attribute values with “double quotes”

Thank you so much for the help, obviously you can see I’m a realy newbie when it comes to javascript. I could easily do all of this in php, but my work won’t install it:(.

Ahhh, I see. You were hoping to do this

<input name="fid_17" value="<? echo $variable ?>" />

Sorry, javascript takes a bit more work

You could document.write the entire line - btw.

Flawless

Sorry beetle one more question, how could assign the variable to a select box instead of an input box, using your function above?

Thanks again for your help:D.

You can use that same method - and ie ( at any rate ) will use the value to select the right option.

Failing that - for more compatability - change the selectedIndex:

document.forms[0].elements[‘element’].selectedIndex = 5;

Flawless

Actually using document.myForm._fid_14.value=variable leaves the select box blank. I would use your example of using the index value, but I know the actual value of the select box, not the index. Any other suggestions

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

var variable = "option1";

function initInput()
	{
	var selectObj = document.forms[0].fid_14;
	selectObj.selectedIndex = getIndex(selectObj);
	}

function getIndex(tempObj)
	{
	for (var i=0; i<tempObj.length; i++)
		if (tempObj.options[i].value == variable)
			return i;
	}

//-->
</script>
</head>

<body onLoad="initInput()">
	<form>
	<select name="fid_14">
	<option value="option0">Option 0</option>
	<option value="option1">Option 1</option>
	<option value="option2">Option 2</option>
	</select>
	</form>
</body>

Yes - you can loop throught the options to find the right one.


obj = document.forms[0].elements['select'];

for (var i=0;i<obj.options.length;i++){
       if (obj.options[i].value == 'testing value'){
            obj.selectedIndex=i;
            break;
            }
       }

Something like that(ish) :wink:

Flawless

Beetle - glad you’re online…

Give me a half hour or so - i’m working on a s**t hot
bit of work to do with international locations…

( ie putting gatwick in a textbox and it coming back with all the options that might match )

Flawless

Thanks for the help guys I got it working. I have one more question I just don’t understand about javascript…how can I use variables like in php <? echo $variable; ?>.

I have a redirect link that I need to tag a couple variables to the end of.

<INPUT type=hidden value=https://www.blahblah.com/info.html name=rdr>

How can I write a javascript variable to the end so it will be

<INPUT type=hidden value=https://www.blahblah.com/info.html?variable=value name=rdr>

I’m confused:confused:

With javacript, you can’t directly include variables into the HTML. You have basically two options.

  1. Write the entire line with javascript’s document.writeln() and insert variables where needed

  2. Perform everything with events calling functions

Before i get started, you should know that '+ 'is the concat operator in javascript, versus the ‘.’ for PHP.

Example using #1 above

<script>
document.write('<INPUT type=hidden value=https://www.blahblah.com/info.html?variable='+var1+' name=rdr>');
</script>

The above examples assumes that var1 is global.

Example using #2 above

<script>
function setHidden()
	{
	document.forms[0].rdr.value += '?variable=' + var1;
	return true;
	}
</script>
</head>
<body>
<form onSubmit="return setHidden()">
<INPUT type=hidden value=https://www.blahblah.com/info.html name=rdr>
</form>

Again, this example assumes var1 is global

Did I explain it all ok? Ask away if you have more questions.

yes it makes perfect sense…option one seems a lot easier. So I can use document.write anywhere in an html file as long and i place it within <script></script>…correct? Are there any other rules for document.write I should know about?

Correct, anytime within <script></script> tags. However, it’s standard to declare them this way <script language=“javascript” type="text/javascript>. I don’t know about you, but that’s alotta typing for every script. So, skip that silly nonsense by including this META tag.
<META HTTP-EQUIV=“Content-Script-Type” CONTENT=“text/javascript” />

Oh, and you should note the difference between

  1. document.write();
  2. document.writeln();
    Wherein #2 creates a linebreak in the run-time HTML, and #1 does not.

php has a function urlencode to replace spaces with %20, does javasript have something like this.

Yes!
escape(string);
-and-
unescape(string);