SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Getting the value from a function

    Hello and good morning!

    I'm working on a custom function and I'm not sure how to get the value from it and submit the result of that function into the database. If I put response.write it only writes it to the screen.

    Here's the code:
    Note that the function is in an external file which is being included at the top of the page but for example purpose I've embedded it.

    ASP:

    Code ASP:
     
    Dim Action
    Action = Request.Form("Action")
     
    Function vbOWE(str)
     
         strLen = len(str)
     
         For i = 1 To strLen
     
              singles = Mid(str, i, 1)
     
              'This is the value I want but without having to use response.write
              Response.Write (Replace(singles, singles, "-"))
     
         Next
     
    End Function
     
     
    If Action = "Submit" Then
         'Getting value from Form:
         Dim input
         input = Request.Form("input")
     
         'When this is triggered it only write the function result to the page, not to database
         conn.Execute("UPDATE [table] SET Password='" & vbOWE(input) & "' WHERE ID=1")
    End If

    HTML:

    HTML Code:
    <form>
         <input type="text" name="input" id="input" value="" />
         <input type=submit" name="Action" id="btnSubmit" value="Submit" />
    </form>
    Last edited by sd_javascript; Nov 3, 2010 at 08:05. Reason: Clarification

  2. #2
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    689
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    change

    Code ASP:
    Response.Write (Replace(singles, singles, "-"))

    to

    Code ASP:
    vbOWE=(Replace(singles, singles, "-"))

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is how you use a function to return a value:
    Code ASP:
    Function vbOWE(str)
     
         'Do some code
     
         'Return the value of the function
         vbOWE = "The result of Do some code"
     
    End Function
     
    newstr = vbOWE ("Some Data")
    Your usage of the vbOWE function is strange - the Write line is happening inside a loop ... you cannot return a function value from within a loop as the function (hence loop) will exit. What is it that you are actually tring to do? Make the user input SQL-safe? Or something different?

    Check out the stickies at the top of this forum - there's probably a function in there already written that does what you're after.

    @webber - that won't really do anything, other than show the method for returning data from a function.
    Ian Anderson
    www.siteguru.co.uk

  4. #4
    SitePoint Zealot ok_hornet's Avatar
    Join Date
    May 2009
    Location
    Oklahoma
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know if you need to have it done, but are you setting/opening a connection to your database before all of this?

    Also, depending on what database you're using (definitely with MSAccess) the term "password" is a reserved term. If it's worked for you in the past then forget what I'm saying, but if this is the first time trying this, you may want to change the column to say "password1" or something.
    Follow me: @josh_max

  5. #5
    SitePoint Zealot ok_hornet's Avatar
    Join Date
    May 2009
    Location
    Oklahoma
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops, listen to them, they know more!
    Follow me: @josh_max

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    This is how you use a function to return a value:
    Code ASP:
    Function vbOWE(str)
     
         'Do some code
     
         'Return the value of the function
         vbOWE = "The result of Do some code"
     
    End Function
     
    newstr = vbOWE ("Some Data")
    Your usage of the vbOWE function is strange - the Write line is happening inside a loop ... you cannot return a function value from within a loop as the function (hence loop) will exit. What is it that you are actually tring to do? Make the user input SQL-safe? Or something different?

    Check out the stickies at the top of this forum - there's probably a function in there already written that does what you're after.

    @webber - that won't really do anything, other than show the method for returning data from a function.
    What I'm trying to do is create my own 'MD5=like' function. So, when a user changes their password or enters it into the login screen, the password is converted into this random string of characters (inside the function) and either compared against or inserted into the database.

    The reason it's in the loop like that is because I'm pulling apart the entire string, character by character and replacing it with a random string of alphanumeric characters.

    I'll have a look at the stickies but if you have any other ideas, let me know.

    Thanks for your replies!!

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to siteguru, this problem has been solved.

    After giving me the solution on how to get the value of the function (vbOWE = newStr), he also pointed out that it wouldn't be useful being inside a loop. So, in order to fix that, I created a variable that would append to itself after each loop then set the function return to that variable (newStr). I can now insert the 'encrypted' string, in full, into the DB.

    Thanks so much for your help!!!

    Here is the code in it's full form:

    Code ASP:
    Private Function vbOWE(str)
     
    	strLen = len(str)
     
    	For i = 1 To strLen
     
    		singles = Mid(str, i, 1)
     
                              'Replace Lowercase
    		If singles = "a" Then charRep = "md23" End If
    		If singles = "b" Then charRep = "dm37" End If
    		If singles = "c" Then charRep = "39fn" End If 
    		If singles = "d" Then charRep = "4dd5" End If
    		If singles = "e" Then charRep = "fg23" End If 
    		If singles = "f" Then charRep = "05we" End If 
    		If singles = "g" Then charRep = "f22g" End If 
    		If singles = "h" Then charRep = "1337" End If 
    		If singles = "i" Then charRep = "4310" End If 
    		If singles = "j" Then charRep = "g5df" End If 
    		If singles = "k" Then charRep = "fw21" End If 
    		If singles = "l" Then charRep = "234g" End If 
    		If singles = "m" Then charRep = "gh3g" End If 
    		If singles = "n" Then charRep = "1fgw" End If 
    		If singles = "o" Then charRep = "2g22" End If 
    		If singles = "p" Then charRep = "vw22" End If 
    		If singles = "q" Then charRep = "25u7" End If 
    		If singles = "r" Then charRep = "87fw" End If 
    		If singles = "s" Then charRep = "dm32" End If 
    		If singles = "t" Then charRep = "few4" End If 
    		If singles = "u" Then charRep = "pro5" End If 
    		If singles = "v" Then charRep = "cvn2" End If 
    		If singles = "w" Then charRep = "74f4" End If 
    		If singles = "x" Then charRep = "6wj4" End If 
    		If singles = "y" Then charRep = "4sg5" End If 
    		If singles = "z" Then charRep = "eht6" End If
     
    		'Replace Uppercase
    		If singles = "A" Then charRep = "4das" End If
    		If singles = "B" Then charRep = "fvwe" End If
    		If singles = "C" Then charRep = "10ee" End If 
    		If singles = "D" Then charRep = "8rth" End If
    		If singles = "E" Then charRep = "645d" End If 
    		If singles = "F" Then charRep = "fwev" End If 
    		If singles = "G" Then charRep = "4fwe" End If 
    		If singles = "H" Then charRep = "few1" End If 
    		If singles = "I" Then charRep = "f324" End If 
    		If singles = "J" Then charRep = "as74" End If 
    		If singles = "K" Then charRep = "654f" End If 
    		If singles = "L" Then charRep = "4w4d" End If 
    		If singles = "M" Then charRep = "23ds" End If 
    		If singles = "N" Then charRep = "021w" End If 
    		If singles = "O" Then charRep = "844w" End If 
    		If singles = "P" Then charRep = "1808" End If 
    		If singles = "Q" Then charRep = "23wd" End If 
    		If singles = "R" Then charRep = "7w72" End If 
    		If singles = "S" Then charRep = "54ef" End If 
    		If singles = "T" Then charRep = "5wq4" End If 
    		If singles = "U" Then charRep = "d5q1" End If 
    		If singles = "V" Then charRep = "6yk9" End If 
    		If singles = "W" Then charRep = "we91" End If 
    		If singles = "X" Then charRep = "fwe4" End If 
    		If singles = "Y" Then charRep = "65ef" End If 
    		If singles = "Z" Then charRep = "4e46" End If
     
    		'Replace Numbers
    		If singles = "1" Then charRep = "654w" End If
    		If singles = "2" Then charRep = "few7" End If
    		If singles = "3" Then charRep = "23f4" End If 
    		If singles = "4" Then charRep = "ykt4" End If
    		If singles = "5" Then charRep = "zq46" End If 
    		If singles = "6" Then charRep = "75ec" End If 
    		If singles = "7" Then charRep = "123d" End If 
    		If singles = "8" Then charRep = "bies" End If 
    		If singles = "9" Then charRep = "8w1w" End If 
    		If singles = "0" Then charRep = "f238" End If 
     
    		newStr = newStr & (Replace(singles, singles, charRep))
     
    	Next
     
    	vbOWE = newStr
     
    End Function
    Last edited by sd_javascript; Nov 3, 2010 at 10:39. Reason: Clarification


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •