Results 1 to 17 of 17

Thread: a different deleting question...

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537

    a different deleting question...

    hi,
    i am doing a delete records page.
    the first page (asp) pulls all the users out of a database(access).

    they are formated in html w/ a checkbox next to them.
    the user selects all the checkbox's and hits the 'delete' button(a form button)

    this calls the next page which will delete the users that were selected on page one.

    sounds simple enough eh?

    my problem is, i don't know how to 'grab' the users on page two.
    because i never know how many users are on page one, i have to give the check box's a name like
    name="chkBox" & i

    where i is variable that gets increased everytime the loop is run
    i thought i could have a hidden field that is associated with the checkbox and the user.
    so it might look like this
    <input type="hidden" value="<%=user%>" name="hidBox" & i>
    (or something, that syntax might be allittle off but the idea is, it would have the same name as the check box and the value would be the same as the user)

    on page two i was trying to do something like this

    dim user
    user = request("hidBox")

    only i have to add the number to this so i can grab all of them
    but i only want the ones that have been selected.


    if that explanation doesn't make sense, please tell me and i will try and re-explain it.

    basicly, i want to do what hotmail does kinda, where you can select a bunch of records using checkbox's and then delete those records from the database.

    thanks for any suggestions!
    pnj

  2. #2
    Hyperactive Member DKCK's Avatar
    Join Date
    Dec 2000
    Location
    United States
    Posts
    329
    I'm a little confused by the post however I think I understand what you are trying to do. Instead of naming the checkboxes i couldn't you name them with a unique ID for the record being deleted? This way you would know which record to delete.

    Or you could keep the i method. Store the the total number of records in your hidden field. Then when you go to the next page loop for all the records. Check if the checkbox is selected. If selected, delete the record.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    yea, what i'm doing now is assigning the value of the checkbox to the ID field from the database.
    then when the check box is selected i will get back the number of the checkbox that is associated with the name and id from the database.

    only i'm still having trouble.

    this is what i have now on page

    <input type="checkbox" value="<%=(id)%>" name="chkBox">

    on page two i am trying to grab the information like this

    dim strName
    strName=request.form("chkBox")

    i thought it would create a string w/ the values of the selected check box's.

    but instead it does nothing.....

    the information is being passed because i can see it in the query string. but for some reason it isn't being grabed by the request.form


    any more suggestions?

    thanks!
    pnj

  4. #4
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    You are using the wrong approach, and making it harder. This is how i do it.

    Lets say all the users have UserId as a unique identifier.

    Now the simplest way to do it is use a control array, by this i mean you name all the check boxes as the same.

    eg
    page 1.asp
    VB Code:
    1. do while not rs.eof
    2.    response.write rs("Username") & vbtab & rs("Location")  & "<input type=checkbox name=UserId value=" & rs("UserId") & ">Delete<br>"
    3. rs.move next
    4. loop

    Now in page 2
    you can simply get the selected item by saying
    VB Code:
    1. dim uid
    2. uid=request.form("UserId")
    3.  
    4. if isarray(uid) then
    5.     uid=split(uid, ",")
    6.     for i=0 to ubound (uid)
    7.       sql="Delete From User Where UserId=" & uid(i)
    8.       conn.execute(sql)
    9.     next
    10. else
    11.   'user have only selected one item
    12.    sql="delete from user where userid=" & uid
    13.    conn.execute(sql)
    14. end if

    Hope this helps, sorry about the raw coding, just put it together here, might have errors.
    Last edited by Danial; Mar 24th, 2002 at 05:20 PM.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  5. #5
    Hyperactive Member DKCK's Avatar
    Join Date
    Dec 2000
    Location
    United States
    Posts
    329
    I think I see the problem now. Checkboxes only have two values, checked and unchecked. What you should do is name each checkbox by the unique ID. Then you can check each checkbox on the next page by ID. If it is selected, then you know to delete that record.

  6. #6
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Originally posted by pnj
    dim strName
    strName=request.form("chkBox")

    i thought it would create a string w/ the values of the selected check box's.

    but instead it does nothing.....

    the information is being passed because i can see it in the query string. but for some reason it isn't being grabed by the request.form


    any more suggestions?

    thanks!
    You say you can see the querystring that means you are using get method, in that you should use
    strName=request.querystring("chkBox")
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    yea i'm using
    request("chkBox") and that seems to be working.

    Danial, i like your idea. i'll give it a try. it looks alot easier than what i was trying.

    thanks again.
    pnj

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    the isarray thing doesn't seem to be working....

    i am using personal web server on windows 98


    is it a asp 3. thing?

    thanks
    pnj

  9. #9
    Hyperactive Member DKCK's Avatar
    Join Date
    Dec 2000
    Location
    United States
    Posts
    329
    I like my idea the best..

  10. #10
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Originally posted by DKCK
    I like my idea the best..
    Sorry DKCK, you got the wrong idea, I am sure your idea works too, there is alwasy more then way of doing things. i mearly suggested an alternative which i have used frequently and also pnj asked if there was any other way of doing it. No offence was meant to you. And i certainly dont intend to get into argument on whose code is best as i am here to help people and get help not to prove anything . No hard feelings

    Pnj i am not sure about what asp version supports IsArray(), you could use instr function to check if the user selected more then one item.

    Are you sure the error is with Isarray, post your code here if possible.

    Danial
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    well, i assume the problem is with the isarray thing because when i put a response.write line of code into the for next loop (just to see if that part of the code ever runs) it doesn't write anything to the browser.

    so i tried to comment out the if then statement and just run the isarray function.....just to test it.

    here is what my code looks like now

    uid=request.form("chkAddressID")
    Response.Write uid
    'if isarray(uid) then
    uid=split(uid, ",")
    for i=0 to ubound (uid)
    sql="Delete From email Where ID=" & uid
    conn.execute(sql)
    Response.Write "in the array<br>"
    Response.Write sql
    next
    'else
    'user have only selected one item
    ' sql="delete from email where userid=" & uid
    ' conn.execute(sql)
    ' Response.Write "not in the array"
    'end if
    '********

    you can see i commented out a few lines. that is just for testing purposes.
    what i get in the browser is the first response.write uid
    then i get the "in the array" section writen out.

    if i uncomment out the lines, it allways jumps to the ELSE part of the statement.

    another funny thing is, the response.write sql doesn't write anything to the browser....

    thanks again!
    pnj

  12. #12
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    PNJ,
    the first code i posted had few erros in it, i dont think the error is with IsArray

    here is the clean version of it

    VB Code:
    1. <%
    2. uid=request.form("chkAddressID")
    3. if isarray(uid) then
    4.     uid=split(uid, ",")
    5.     for i=0 to ubound (uid)
    6.         sql="Delete From email Where ID=" & uid(i)
    7.          
    8.         Response.Write sql & "<br>"
    9.     next
    10. else
    11.     sql="delete from email where userid=" & uid
    12.     Response.Write sql
    13. end if
    14. %>

    This code is the same but it doesnt use IsArray
    VB Code:
    1. <%
    2. uid=request.form("chkAddressID")
    3. if instr(1, uid, ",") then
    4.     uid=split(uid, ",")
    5.     for i=0 to ubound (uid)
    6.         sql="Delete From email Where ID=" & uid (i)
    7.          
    8.         Response.Write sql & "<br>"
    9.     next
    10. else
    11.     sql="delete from email where userid=" & uid
    12.     Response.Write sql
    13. end if
    14. %>

    Test them out now.

    Hope this helps.

    Danial
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    ok, here is where i am now.

    it seems as if the split function does not work.

    if i do this
    uid= request.form("id")
    uid=split(uid, ",")
    Response.Write uid

    it prints out the same thing as uid. i meen, it is still delimitaed by commas.

    so when it is printed out it looks something like this.

    1,3,8,9

    with those numbers being the checkbox's on page one that were selected.
    so my sql statement allways looks like this

    delete email where ID = 1,3,8,9

    instead of looking like this

    delete emai where ID= 1

    then on the next loop it would look like

    delete email where ID= 3

    etc....

    does that make sense?

    another strange thing is, if i write the sql inside the for next loop.
    it doesn't seem to work.
    in my last post i said i couldn't get it to write to the screen using response.write sql

    but if i take the sql line out of the for/next loop. it will write to the screen. only then it won't add the next ID number.....

    sorrry if that makes no sense. i have made to progress on this thing that should be very easy to get to work.....

    pnj

  14. #14
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Try out my the cleaned up code.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537

    nice!

    yea, that works great.

    thanks!!

    now to get the actual DELETE function to work.....

    i have a connection open to the database so all i should have to do is put
    conn.execute(sql) inside the for/next loop right?

    i'll give it a try...

    thanks again!
    pnj

  16. #16
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: nice!

    Originally posted by pnj
    yea, that works great.

    thanks!!

    now to get the actual DELETE function to work.....

    i have a connection open to the database so all i should have to do is put
    conn.execute(sql) inside the for/next loop right?

    i'll give it a try...

    thanks again!
    you are welcome. sorry about the first post, i just thougt you need a quick ans, so i posted some pseducode without test.

    Yep it should be inside the loop.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  17. #17
    Hyperactive Member DKCK's Avatar
    Join Date
    Dec 2000
    Location
    United States
    Posts
    329
    Actually I like his answer better...

Posting Permissions

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



Click Here to Expand Forum to Full Width