Solved

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

Posted on 2013-01-11
7
1,216 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 52

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 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 52

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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