Solved

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

Posted on 2013-01-11
7
1,179 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
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 video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now