How to execute an SQL in onClick of an HRef ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Matsam
    New Member
    • Jun 2007
    • 33

    How to execute an SQL in onClick of an HRef ?

    Hi,

    I have a page showing the result of a search criteria. I'm using VB script. I have given link to an attached file for each record. There is also a field "Number of downloads" in my table. When a user clicks on a particular link, I want to update (increment by 1) the No. of downloads field in my table, for that particular record. My code is as follows.
    <TD nowrap borderColor=#80 0000 width="220"> <A id=D href= "http://localhost/MyPage/<%= trim(replace(rs (7),"#","")) %>" title="Click here to view full article"
    onclick=<% sqlQuery = "Update Book set NoOfDownloads = NoOfDownloads+1 where SlNo = " & rs(9)
    cn1.Execute sqlQuery %> >

    where rs(7) is the attached file name and rs(9) is the SlNo(PK).
    But this update query is executed every time the page is loaded, evenif not clicked. Also it is updating for all the records having attachments.

    Plz help me.

    Matsam
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    Matsam,

    ASP only runs when the page is being loaded. That is the only time it runs. Period. The code executes wherever it is written, regardless of being in an onClick event or whatever and considers everything outside of the asp code tags to just be static HTML it needs to print to the page. Therefore, putting an asp code inside an onClick event is completely meaningless. The script will execute the command while loading.

    If you want to run an asp script on an onClick event try using a pop-up window or an iframe which you can reload while the rest of the page stays the same. Put the script you need to run in the pop-up window and when it is done you can have the pop-up window interact with the parent window thru javascript (window.opener I believe) if you need to pass values back, then close the pop-up window if you like.

    Does this answer your question?

    Jared

    Comment

    • Matsam
      New Member
      • Jun 2007
      • 33

      #3
      Originally posted by jhardman
      Matsam,

      ASP only runs when the page is being loaded. That is the only time it runs. Period. The code executes wherever it is written, regardless of being in an onClick event or whatever and considers everything outside of the asp code tags to just be static HTML it needs to print to the page. Therefore, putting an asp code inside an onClick event is completely meaningless. The script will execute the command while loading.

      If you want to run an asp script on an onClick event try using a pop-up window or an iframe which you can reload while the rest of the page stays the same. Put the script you need to run in the pop-up window and when it is done you can have the pop-up window interact with the parent window thru javascript (window.opener I believe) if you need to pass values back, then close the pop-up window if you like.

      Does this answer your question?

      Jared

      Hi

      Thanks for ur reply.. But sorry... my problem is not solved. Also I am not familiar with popups & iFrames... Any simple method for the same ??
      Or else plz give me the code for displaying popups while clicking a hyperlink.

      Thnks

      Matsam

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        It all depends on what you need to do. Is sending a small bit of data to the dbreally the only thing you need to do? If that is all, then make a script that does this, but put it on a new page. When the user does the important action, say clicking a button or whatever, then open that page. If this action is an existing HTML control, like a form element you will probably need some simple javascript to open the pop up. I am not a javascript expert, but the code is something like:
        [code=html]<input type="button" onClick="window .open('mydataTr ackerPage.asp') ">[/code]Notice, I would put the script for changing the db on the myDataTrackerPa ge.asp, when that page is loaded it will execute the db update. You can close the popup after it has served its purpose with javascript as well, the code is something like this:
        Code:
        <... onLoad="this.window.close()">
        iframes are more complicated and weren't well-supported when I learned HTML, so I never bothered, but I am aware that they can do the same thing, perhaps better, although this is a matter of opinion.

        Jared

        Comment

        • Matsam
          New Member
          • Jun 2007
          • 33

          #5
          Originally posted by jhardman
          It all depends on what you need to do. Is sending a small bit of data to the dbreally the only thing you need to do? If that is all, then make a script that does this, but put it on a new page. When the user does the important action, say clicking a button or whatever, then open that page. If this action is an existing HTML control, like a form element you will probably need some simple javascript to open the pop up. I am not a javascript expert, but the code is something like:
          [code=html]<input type="button" onClick="window .open('mydataTr ackerPage.asp') ">[/code]Notice, I would put the script for changing the db on the myDataTrackerPa ge.asp, when that page is loaded it will execute the db update. You can close the popup after it has served its purpose with javascript as well, the code is something like this:
          Code:
          <... onLoad="this.window.close()">
          iframes are more complicated and weren't well-supported when I learned HTML, so I never bothered, but I am aware that they can do the same thing, perhaps better, although this is a matter of opinion.

          Jared

          Hi

          Thanks a lot... As suggested by u, I placed a new page for download in between and managed to solve my problem.
          Thanks again

          Matsam

          Comment

          Working...