call procedure from onclick event of button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chirag1989
    New Member
    • Jan 2008
    • 28

    call procedure from onclick event of button

    hi
    i have some textboxes in my form wat i want to
    do is wheever the user clicks the button
    the data in text boxes must be stored in database
    i have written my insert query like this

    <script="vbscri pt">
    <--
    sub procedure cmd1_onclick()
    <%
    set con=server.crea te.....
    con.open "....."

    conn.execute "Insert into........... ."
    %>
    end sub

    -->
    </script>

    the problem is whenever the page is loaded the insert query is executed
    so a blank record is inserted in database and then when button is clicked again
    the data is inserted
    the problem is its executin the asp code in deleminators first on loadin a form
    but i want that to be executed only when click event is fired for button
    i only know vbscript
    plz help me out
    i have temporarily solved this by placin a submit button
    and in its action called a form where data from textbox is requested
    and then stored in database through query there and again redirecting to
    first form
    this is increasing my forms nd indeed is a wrong way
    so plz help me out
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi there,

    There is nothing wrong with the way you are updating the database now! Requesting your input values, running the update and returning to the original page is a perfectly acceptable way of updating your database.

    The problem you are having using a VBScript function is you have wrapped it in ASP tags and ASP runs on the server. The OnClick event occurs on the client so it can only call client side code. If you want to use a vbscript function then don't wrap it in ASP tags. e.g.

    Code:
    <html>
    <head>
    <script language="vbscript"> 
    	sub TestVBScript()
    	 msgbox("The time is " & Time) 
    	end sub
    </script>
    </head> 
    <body> 
    <form> 
    <input type="button" value="Click" onclick="call TestVBScript()"> 
    </form>
    </body> 
    </html>
    Remember, you will not be able to update your database using client side functions as doing this requires ASP which is a server-side language.

    I'd stick with the method you're already using.

    I hope this helps,

    Dr B

    Comment

    • chirag1989
      New Member
      • Jan 2008
      • 28

      #3
      thanks sir
      i have another question that
      when the user fills textbox and clicks on submit button leaving one textbox empty then a msgbox should be shown to ask him to fill all textboxes and when he clicks on ok he should be taken to first form where only the textbox whose values were not filled should be empty other textboxes should have their original values

      Comment

      • DrBunchman
        Recognized Expert Contributor
        • Jan 2008
        • 979

        #4
        Hi Chirag,

        No problem. You can do this using Javascript or VBScript but as you're comfortable with VBScript we'll use that. I've commented the code below so hopefully you can see what it is doing:

        Code:
         
        <html>
        <head>
        <script language="vbscript"> 
        	sub TestVBScript()
         
        	 'CHECK WHETHER ANY OF THE TEXT BOXES ARE EMPTY (YOU CAN ADD AS MANY TEXT BOXES AS YOU NEED TO THIS CHECK)
        	 If Document.TestForm.txtBox1.Value = "" Or Document.TestForm.txtBox2.Value = "" Then
         
        	 ' IF ANY ARE EMPTY THEN DISPLAY A MESSAGE TO THE USER
        	 msgbox("Please fill in all text boxes!")
         
        	 ' THEN RETURN FALSE TO THE FORM SUBMIT (PREVENTS THE FORM FROM SUBMITTING)
        	 window.event.returnValue = false
        	 Else
         
        	 'IF ALL TEXT BOXES CONTAIN TEXT THEN RETURN TRUE (ALLOWING THE FORM TO SUBMIT)
        	 window.event.returnValue = true
        	 End If
        	end sub
        </script>
        </head> 
        <body> 
        <form name="TestForm">
        <input type="Text" name="txtBox1">
        <input type="Text" name="txtBox2">
        <input type="submit" value="Click" onclick="call TestVBScript()"> 
        </form>
        </body> 
        </html>
        The important thing here is the window.event.re turnValue. The OnClick event is fired before the form is submitted so by returning "false" we can prevent the form from doing so. Using this value you can perform all sorts of validation on your form prior to it being submitted.

        I hope this helps and if you need any further help then please let me know.

        Dr B

        Comment

        Working...