how to check if a variable exists or isset in asp?
Discuss how to check if a variable exists or isset in asp? in the ASP Programming forum on Dev Shed. how to check if a variable exists or isset in asp? ASP Programming forum discussing Active Server Pages coding techniques and problem solving methods. Use VBScript or Jscript to make dynamic web applications.
Posts: 14,235
Time spent in forums: 1 Month 4 Weeks 14 h 27 m 16 sec
Reputation Power: 4445
I don't think there is a similar VBScript function to isset, at least not one that comes to mind. I generally use Option Explicit in asp code so there aren't any undefined variables in code.
__________________
======
Doug G
======
It is a truism of American politics that no man who can win an election deserves to. --Trevanian, from the novel Shibumi
Posts: 1,371
Time spent in forums: 4 Days 51 m 34 sec
Reputation Power: 50
In ASP, I typically use Len(variable)>2, or any number safe enough to make assumptions on. Typically, if a recordset or form variable is undefined, this will return 0, where in PHP, an error is thrown, thus the need for isset.
Posts: 365
Time spent in forums: 10 h 45 m 38 sec
Reputation Power: 10
so try len(var)>0
what ive got working is to find whether a form has been posted
in php i have used:
isset($_POST)
and
$_SERVER['REQUEST_METHOD'] == "post"
but i also use isset alot in php, to check if a variable has been set, if not then either create it or do something
what will Option Explicit do if i used it?
the: NOT isnull(var) option doesnt seem to work as an asp alternative to isset but could this be due to not setting Option Explicit?
Posts: 1,371
Time spent in forums: 4 Days 51 m 34 sec
Reputation Power: 50
I've had no luck with the isnull function in the past. ASP seems to have strange ideas about the length and type of a variable pulled from a database.
Option Explicit requires that all variables be defined before they are called by using Dim. Normally ASP does not require this, and many lazy coders (me included) don't use it so that they don't have to write out every variable that they're going to use in the page. Info on Option Explicit
To check whether a form has been posted, you can either do
If Len(Request.Form("fieldName")>1 Then
as long as that fieldName corresponds to a field you are certain will be populated.
Or, add a senseless URL variable to the form's action, like:
<form action="post.asp?submit=1">
Then:
If Request.QueryString("submit")=1 Then
Posts: 371
Time spent in forums: 19 h 32 m 27 sec
Reputation Power: 10
I think ISEMPTY can be used
Cut and paste from the quote in ur ASP page and check what is the result
Quote:
dim vstr
vstr="Hello"
if isempty(vstr) then
Response.write "<br>V is empty <br>"
else
Response.write "<br>V is not empty <br>"
end if
//---vstr1 is not declared and assigned
if isempty(vstr1) then
Response.write "<br>V1 is empty <br>"
else
Response.write "<br>V1 is not empty <br>"
end if
ISEMPTY can be used as follows
Quote:
isEmpty(Request.Form("control_name")) for the existance of a single field
isempty(Request.Form) can be used to check whether the form has been submitted or not
__________________
Regards, Brightlight
Last edited by brightlight : November 9th, 2004 at 10:34 PM.