Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using buttons to Add,Edit, Delete 1

Status
Not open for further replies.

Airpan

Technical User
Jun 14, 2005
172
US
I have built a login page via ASP. Once logged in you are taken to the admin page which displays all of the users in the database. At the bottom of the user table(which lists all the users & their login info), there are three buttons, add, edit, delete. I am running into a bit of trouble with the buttons and would like to know if perhaps it would be easier to use text links? To be very honest, I have never used buttons for specific functions within ASP.

Can someone please help me with the concept of how best to tackle this? In other words, shall I add the buttons' functions as variables at the beginning of all my asp code? Or is it easier to do just add text links and write separate pages of code to perform the functions? I am trying to wrap my mind around it one bite at a time. If anyone needs to see how I am connecting to the DB here is it is:

Code:
set Con = Server.CreateObject("ADODB.Connection")
Con.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("db/logins.mdb")

SQL = "SELECT * FROM cust_logins"

set rec=Con.execute(sql)

and then I use the Response.Write statements to organize the data. I am going to go chase my tail for a while.
 
Chris,
I could use VB to accomplish this right?
 
Most important thing to remember is --> the only things the server knows about the browser are things that the browser has told it as part of the HTTP Request.

Your ASP has already run and is no longer running before the web browser displays the buttons to the user. The button click event happens in the browser. The server has no direct knowlege of the click or anything that happens in the browser.

One approach to solving the 3 buttons issue is to have two separate ASPs. The first ASP connects to the database and builds your user list as part of an HTML form as so maybe something like:[tt]
<form method="post" action="Page2.asp">
<% Do While Not rec.EoF %>
<input type="radio" name="UserID" value="<%= rec("UID")%>">
<%= rec("Name")%>
<br>
<%
rec.MoveNext
Loop
%>
<input type="submit" name="SubmitButton" value="Add">
<input type="submit" name="SubmitButton" value="Edit">
<input type="submit" name="SubmitButton" value="Delete">
</form>[/tt]
When user clicks one of the 3 buttons, the form is submitted which causes an HTTP Request to be sent to the server and this request will contain information as to which button was clicked.

Then on Page2.asp, the code processes the submitted form. By checking the value of Request.Form("SubmitButton") in your ASP, you can determine which of the 3 form buttons was clicked. Also this example the first page determines which user to Add/Edit/Delete by making the HTML form so that it contains one radio button per user and each radio button has the same "name" but a unique "value". So page2.asp looks something like this:[tt]
<%
'Which user?
Dim ThisPerson
ThisPerson = Request.Form("UserID")
If ThisPerson = "" Then
'error
Response.Write "Error: Missing Input [UserID]"
Response.End
End If

Select Case Request.Form("SubmitButton")
Case "Add"
'user clicked Add button

Case "Edit"
'user clicked Edit button

Case "Delete"
'user clicked Delete button

Case Else
'error
Response.Write "Error: Missing Input [SubmitButton]"
Response.End
End Select
%>
[/tt]

Ultimately this does not need to be two separate ASPs. In the long run you may prefer to have one ASP with a form that submits to itself. To simplify development, it may be best to get it working as two pages and then merge them once the core functions are performing to your specifications.
 
Sheco,
I think I have something set up similar to what you are suggesting. I have the login page, it then directs you to a page which list the current users. As you suggest, at the bottom of that same page, I have a form that asks for the information to be entered, with the three buttons displayed. Here is my code so far (I am still writing this code and am in the process of declaring my variables from the form):
Code:
<%
Dim custid, custname, custuser, custpass, custemail, add, edit, remove 
custid=TRIM(Request("CustID"))
addcust=Request("add")="addcust"
editcust=Request("edit")="editcust"
remcust=Request("remove")="remcust"

if addcust then
   Dim Con, sql, rec
   set Con = Server.CreateObject("ADODB.Connection")
   Con.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("db/logins.mdb")
   
  SQL = "INSERT INTO cust_logins (CustID, CUSTUSER, CUSTPASS, CustName, email, TruckNo)
         VALUES (

%>
</head>
<body>
<form action="pullcust.asp" method="post" name="Form">
Customer ID: 
    <input name="custid" type="text">
    <br>
Customer Name: 
<input name="custname" type="text">
<br>
Customer Username: 
<input name="custuser" type="text">
<br>
Customer Pass:  
<input name="custpass" type="text">
<br>
Customer Email: 
<input name="custemail" type="text"><br>
<input name="add" type="button" value="Add">
<input name="edit" type="button" value="Edit">
<input name="remove" type="submit" value="Delete">
</form>
I have not completed my SQL yet either, as I am feeling a bit overwhelmed. I can't help but think I am over complicating this somehow. My ultimate goal is similar to yours, but wanted to use if then statements to allow for which button is pressed and what will happen with the form data. Is that wrong?
Any help is still appreciated and thanks Sheco for making it a bit more laid out. Sometimes it is easier to understand when it is laid out as you would write the code, but not necessarily the exact code.
 
[...] wanted to use if then statements to allow for which button is pressed and what will happen with the form data. Is that wrong?
It is not wrong. It is a matter of personal preference and style.


The following two examples will have exactly the same result:
Code:
IF Request("TheValue") = "A" THEN
  result = 1
ELSE
  IF Request("TheValue") = "B" THEN
    result = 5
  ELSE
    IF Request("TheValue") = "C" THEN
      result = 8
    ELSE
      result = 0
    END IF
  END IF
END IF

Response.Write "The result is: " & result


Second example:
Code:
SELECT CASE Request("TheValue")
  CASE "A"
    result = 1

  CASE "B"
    result = 5

  CASE "C"
    result = 8

  CASE ELSE
    result = 0
END SELECT

Response.Write "The result is: " & result

Personally I tend to get confused by multiple nested IF/THEN conditions so I try to use the SELECT CASE syntax when there are 3 or more options but you can choose whichever technique is most comfortable for you.
 
Sheco,
I'm a moron... I just looked at your code a little closer. I am going to give what you said a try. I was complicating the buttons... you wrote the if then statements to do exactly what I need. Aiy yih yih - back to the coffee pot.
 
In your example code you have 2 regular buttons and one submit button:
[tt]
<input name="add" type="button" value="Add">
<input name="edit" type="button" value="Edit">
<input name="remove" type="submit" value="Delete">[/tt]

My suggestion is that you make all three buttons into submit buttons. This will work if you give all three submit buttons the same "name" so that, when the form is submitted, the browser will send a HTTP Request to the server containing a "name/value" pair for the submit button.
[tt]
<input name="SubmitButton" type="submit" value="Add">
<input name="SubmitButton" type="submit" value="Edit">
<input name="SubmitButton" type="submit" value="Delete">
[/tt]

Then on your Active Server Page named "pullcust.asp" the code will answer this question: "What value is associated with the name "SubmitButton"?

The answer to this question will be the button which was clicked to cause the browser to submit the form.
 
Sheco,
I got most of the pages to work, but there is still one kink. Just so you can understand my layout a bit better, as I think this will clarify some things, the "chain" is as follows:
Login page takes you to admin1.asp. Admin1 displays the customers, but their user id is displayed as a hyperlink. This hyperlink utilizes pullcust.asp to pull JUST that customer from the DB. In short, only that customer will be displayed with the new buttons I added.

I added your form code to the pullcust page and changed the buttons as you suggested. However, when I press any of the buttons, I get an error regarding my SQL query. So, here is the updated code from pullcust.asp:
Code:
<%
	customer=Request.QueryString("customer") 
	
	Set Con = Server.CreateObject("ADODB.Connection")
	Con.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("db/logins.mdb")

	SQL = "SELECT * FROM cust_logins WHERE [CustID] = " & customer & " "
	
	Set rec=Con.execute(sql)

Response.Write("<table border=1><tr><th>CustomerID</th><th>Cuser</th><th>CPass</th><th>CustName</th><th>Email</th></tr>")

    Do While Not rec.EOF

Response.Write("<tr>")
For x=0 to rec.Fields.Count-1
Response.Write("<td>")
Response.Write(rec(x))
Response.Write("</td>")
Next
Response.Write("</tr>")
rec.Movenext
Loop
Response.Write("</table>")
Response.Write("<br>")
%>
<html>
<head>
<title>This is so not going to work</title>
</head>
<body>
<form method="post" name="Form" action="pullcust.asp">
<% Do While Not rec.EoF %>
<input type="radio" name="custid" value="<%= rec("CustID")%>">
<%= rec("CustName")%>
<br>
<%
  rec.MoveNext
Loop

con.Close
Set con=Nothing
%>
<input type="submit" name="SubmitButton" value="Add">
<input type="submit" name="SubmitButton" value="Edit">
<input type="submit" name="SubmitButton" value="Delete">
</form>
</body>
</html>
The error I get is:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression '[CustID] ='.
/aspdevelopment/WasteConn/pullcust.asp, line 10
.
Not withstanding the SQL error, does the rest of the code look as it should?
I am still working on getting the buttons to function in this way:
1. The Add and Edit buttons will allow you to fill in a form below the displayed record that will insert the new information into the database (for that record only), and/or allow the current information in the database to be updated.
2. The delete button to delete the row of information in the database. I know that SQL will be a large part of this, but just want to be sure that I get the buttons to do their job first? Thanks again Sheco.
 
LOL, Sheco, after I posted all of the above, I got it to work... it is the second page that now generates an error and it is the one that we asked it to generate if it is missing information, which by the way I called admin2.
Error: Missing Input [UserID]

For whatever reason it is not displaying the radio button either on the first page (pullcust.asp).
 
Sheco,
Thanks for all your help, but I think i am going to try another way to do this. After talking with a friend, I think I may have worked out another solution. Thanks so much again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top