Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setting a variable equal to the value of a textbox on an ASP page

Posted on 2013-01-11
7
Medium Priority
?
1,433 Views
Last Modified: 2013-01-16
I am needing to set a variable = to a textboxes value.

for instance i want:

Var1 = txtbox1.value but when I do that I get:

Microsoft VBScript runtime error '800a01a8'

Object required: ''

/energysystc/electricalppmChlorAlkali.asp, line 1573

and if I do:

 Var1 = txtbox1
the response.write returns nothing.

I am just looking for the proper sytax or method on doing this.
0
Comment
Question by:jlcannon
  • 3
  • 3
7 Comments
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 38766920
Will you please give some example code.

Do you want the the variable after the form is submitted?

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
var1=""
if request("textbox1")<>"" then
	var1=request("textbox1")
end if
%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<h1>Var1 = 
<%
if var1<>"" then
	response.write var1
	else
	response.write "N/A. Please submit the form."
end if
%>
</h1>
<body>
<form name="form1" method="post" action="">
  <label for="textbox1"></label>
  <input type="text" name="textbox1" id="textbox1">
  <input name="Submit" type="submit" value="Submit">
</form>
</body>
</html>

Open in new window

0
 
LVL 54

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 total points
ID: 38767094
If you want to get the value of the the text box before the form is submitted you need to use javascript and lets say you will put that to a hidden field and a div.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
var1=""
if request("textbox1")<>"" then
	var1=request("textbox1")
	hiddenfield=request("hidden1")
end if
%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script>
var1=    document.getElementById("textbox1").value;
document.getElementById("hidden1").value=var1;
document.getElementById("hiddenvalue").innerHTML=var1;
</script>
</head>
<h1>Var1 = 
<%
if var1<>"" then
	response.write var1
	response.write ". The vaule of the hidden field is "&hiddenfield
	else
	response.write "N/A. Please submit the form."
end if
%>
</h1>
<body>
<form name="form1" method="post" action="">
  <label for="textbox1"></label>
  <input type="text" name="textbox1" id="textbox1" value="hello">
  <input type="hidden" name="hidden1" id="hidden1">
  
  <input name="Submit" type="submit" value="Submit">
</form>
<h2>Just the value in a div</h2>
  <div id="hiddenvalue"></div>
</body>
</html>

Open in new window

0
 

Author Comment

by:jlcannon
ID: 38767115
When I do the above it returns "N/A. Please submit the form" but there is clearly a number i the textbox so not sure why its not grabbing the number.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 38767291
I am used to doing this in jquery and it probably just needs a function.  What is it exactly you are trying to do.  "I am needing to set a variable = to a textboxes value."  Can you please give some sample code.

Are you trying to get a vbscript variable a value without submitting a form?  If so, what is the variable going to be used for?
0
 

Author Comment

by:jlcannon
ID: 38767384
<td width="80%" class="tablesmall">
<input type="text" size="5" name="PPMNumber" id="PPMNumber" value=<%=rs.fields("Off-Line Inspection Period") %>>
<input type="hidden" size="5" name="OLInspect1" value=<%=rs.fields("Off-Line Inspection Period") %>>

<%
 
   
    PPMNum3 = OLInspect1
    response.write PPMNum3
0
 
LVL 18

Expert Comment

by:nap0leon
ID: 38774723
The input does not exist during server side execution.

If you are looping through rs.fields to set the values in your table and want to be able to use those values later on during your server-side script but do not want to keep the record set open, you should assign those items to a variable that can be referenced later.

(While you are writing out each row):
<% OffLineInspectionPeriod_1 = rs.fields("Off-Line Inspection Period") %>
<td width="80%" class="tablesmall">
<input type="text" size="5" name="PPMNumber" id="PPMNumber" value=<%=OffLineInspectionPeriod_1%>>
<input type="hidden" size="5" name="OLInspect1" value=<%=OffLineInspectionPeriod_1 %>>

...

<br/>OffLineInspectionPeriod_1 is still valid later: <b><% Reponse.Write OffLineInspectionPeriod_1 %></b>

Open in new window




Or... use JavaScript to get the values that you placed into those specific fields:
<form name="goober">
<td width="80%" class="tablesmall">
<input type="text" size="5" name="PPMNumber" id="PPMNumber" value=3>
<input type="hidden" size="5" name="OLInspect1" value=3>
</td>
</form>
The value of the hidden input named "OLInspect1" is: 
<script>
oForm = document.forms.goober;
document.write(oForm.elements["OLInspect1"].value);
</script>

Open in new window

0
 

Author Closing Comment

by:jlcannon
ID: 38782547
works great, thank you
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Loops Section Overview
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question