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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing on current page 1

Status
Not open for further replies.

apex82

Programmer
Mar 2, 2009
127
0
0
GB
I am editing records from a SQL database using a form in ASP.

The form posts to a new page

Code:
<form method="post" action="edit_Business.asp">

which contains the following:

Code:
<%
  sql="UPDATE Business SET "
  sql=sql & "[Business Code]='" & Request.Form("BusinessCode") & "',"
  sql=sql & "[Business Description]='" & Request.Form("BusinessDescription") & "',"
  sql=sql & "[UnitID]='" & Request.Form("Unit") & "'"
  sql=sql & " WHERE [Business_ID]='" & Request.Form("Business") & "'"

  Set obj_RS1 = obj_CN.Execute(sql, adBoolean)
%>

Does anyone know how I could incorporate this code within my current page without having to post to another page or leave the current page?
 
DonNetGnat said:
you need to do self-referencing...look at the below url for an example:

That required a post back to the current page, which means you leave the page and come right back to it.

I personally don't like self-referencing a form action as it "kills" the back button - also, if you are inserting records into a db, an page refresh with "F5" could add an extra record to your db (in or in this case, do another update to the db when it is not needed).

If AJAX is not an option, I would use this approach:

Code:
<%
if request.querystring("update") = "complete" then
response.write "the db has been updated"
end if
%>
<form action="page2.asp" method="post">
<input type="text" name="tbxName" />
<input type="text" name="tbxId" value="1" /><br />
<input type="submit" />
</form>

Code:
<%
username = request.form("tbxName")
userId = request.form("tbxId")
sql = "update myTable set user_name = '" & tbxName & "' where user_id = " & userId
'execute my sql / close the recordset
response.redirect "page1.asp?update=complete"
%>



--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
Thanks very much for your help.

I read up on Ajax, but I can’t find anywhere example that inserts a record into a SQL database.

Is this pretty straightforward?

Basically I will have a page with a form:

As far as I can see I just need add code into the bit // code

Code:
<!-- #include file="inc_params.asp" --> 
<!-- #include file="inc_opendb.asp" -->

<html>
</head>
<body>
<h1>Page</h1>
<script type="text/javascript">
function ajaxFunction()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
  [COLOR=red]// code[/color]
  }
}
xmlhttp.open("GET","add_Business.asp",true);
xmlhttp.send(null);
}
</script>
<form name="myform" >
  <table width="800"  border="0" cellspacing="10" cellpadding="0">
    <tr>
      <td width="99">Enter Code:</td>
      <td width="197">Enter Description:</td>
      <td width="131">Enter Unit:</td>
      <td width="145"> ----- </td>
      <td width="168"> </td>
    </tr>
    <tr>
      <td width="99"><input name="BusinessCode" type="text" size="2" maxlength="2"></td>
      <td width="197"><input name="BusinessDescription" type="text" size="40" maxlength="35"></td>
      <td width="131"><select name="Unit" id="Unit">
        <%
	' Unit
	
	str_SQL = "SELECT * FROM [Unit] ORDER BY [Unit_ID], [Unit Code] ASC;"
	Set obj_RS1 = obj_CN.Execute(str_sql, adBoolean)
		
    ' Build the menu
	While Not obj_RS1.EOF
		
		str_UnitID = obj_RS1("Unit_ID")
		str_Unit = obj_RS1("Unit code")
        str_Option = "<option value=""" & str_UnitID & """#Selected#>" & str_Unit & "</option>"
        Response.Write vbtab & str_Option & vbcrlf
	    obj_RS1.MoveNext
	Wend
%>
      </select></td>
      <td width="145">-----</td>
      <td width="168"><input type="submit" name="Submit" value="Submit" onclick="ajaxFunction();" ></td>
    </tr>
  </table>
</form>
</body>
</html>

This is the add_Business.asp page that updates the database.

Code:
<!-- #include file="inc_params.asp" -->
<!-- #include file="inc_opendb.asp" -->
<%

str_SQL = "INSERT INTO Business ([Business Code], [UnitID], [Business Description])" 
str_SQL = str_SQL & " VALUES "
str_SQL =str_SQL & "('" & Request.Form("BusinessCode") & "', '" & Request.Form("Unit") & "', '" & Request.Form("BusinessDescription") & "')"


Set obj_RS1 = obj_CN.Execute(str_sql, adBoolean)

%>
<%
Response.Write("updated")
%>


 
I've spent some time on this and still can't figure out how to "pass" the information to the add_Business.asp page (the page which updates the database).

Can anyone help?

Thanks.
 
Thanks virvirk. That would be fantastic.
 
Assuming "add_Business.asp" works, make these changes (all highlighted in red below) and test it out - I didn't test it, but *think* I got everything. Please double check all the CaSe SeNSiTiViTY.

1. give all your input boxes "id" attributes - call the id the same as the "name"
2. in your ajax function, grab these values
3. build a string with these values (params) basically building a querystring
4. change the "GET" to a "POST" in your xmlhttp.open request
5. add the proper header information to your request to mimic a form post instead of a get
6. send the paramaters in your reqeust
7. you should check the "status" as well as the readystate
8. create a div tag to hold the result of your request.
9. write the result of the request into the div tag
10. return a value of false to ensure the form doesn't submit
Code:
<!-- #include file="inc_params.asp" --> 
<!-- #include file="inc_opendb.asp" -->

<html>
</head>
<body>
<h1>Page</h1>
<script type="text/javascript">
function ajaxFunction()
{
var xmlhttp;
[COLOR=red]
var strBusinessCode = document.getElementById("BusinessCode");
var strBusinessDescription = document.getElementById("BusinessDescription");
var strUnit = document.getElementById("Unit").value;
var strParams = "BusinessCode=" + strBusinessCode;
strParams += "&BusinessDescription=" + strBusinessDescription;
strParams += "&unit=" + strUnit;
[/color]
var 
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 [COLOR=red]&& xmlhttp.status == 200[/color])
  {
  [COLOR=red]
  document.getElementById("divResult").innerHTML = xmlhttp.responseText;
  [/color]
  }
}
xmlhttp.open([s]"GET"[/s][COLOR=red]"POST"[/color],"add_Business.asp",true);
[COLOR=red]
xmlhttp.setRequestHeader("Content-type", "application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
[/color]
xmlhttp.send([s]null[/s][COLOR=red]strParams[/color]);
}
</script>
[COLOR=red]
<div id="divResult"></div>
[/color]
<form name="myform" >
  <table width="800"  border="0" cellspacing="10" cellpadding="0">
    <tr>
      <td width="99">Enter Code:</td>
      <td width="197">Enter Description:</td>
      <td width="131">Enter Unit:</td>
      <td width="145"> ----- </td>
      <td width="168"> </td>
    </tr>
    <tr>
      <td width="99"><input [COLOR=red]id="BusinessCode"[/color] name="BusinessCode" type="text" size="2" maxlength="2"></td>
      <td width="197"><input [COLOR=red]id="BusinessDescription"[/color] name="BusinessDescription" type="text" size="40" maxlength="35"></td>
      <td width="131"><select name="Unit" id="Unit">
        <%
    ' Unit
    
    str_SQL = "SELECT * FROM [Unit] ORDER BY [Unit_ID], [Unit Code] ASC;"
    Set obj_RS1 = obj_CN.Execute(str_sql, adBoolean)
        
    ' Build the menu
    While Not obj_RS1.EOF
        
        str_UnitID = obj_RS1("Unit_ID")
        str_Unit = obj_RS1("Unit code")
        str_Option = "<option value=""" & str_UnitID & """#Selected#>" & str_Unit & "</option>"
        Response.Write vbtab & str_Option & vbcrlf
        obj_RS1.MoveNext
    Wend
%>
      </select></td>
      <td width="145">-----</td>
      <td width="168"><input type="submit" name="Submit" value="Submit" onclick="ajaxFunction();[COLOR=red]return false;[/color]" ></td>
    </tr>
  </table>
</form>
</body>
</html>


--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
Thanks vicvirk, really appreciate it.

I've copied the changes.

I'm probably missing something simple but it doesn't seem to update the database or show anything in the <div>.

Am I missing anything?

Thanks.
 
do you get any javascript errors?

Also, can you confirm that the page that is being called "add_Business.asp" works without any errors?


--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
^

also, I did make at least one error I in the version I posted:

Code:
xmlhttp.setRequestHeader("Content-length", [COLOR=red]strP[/color]arams.length);


--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
I’m not getting any javascript errors.

The add_Business.asp page works when I use the following code to post to it. This is from a separate test page.

Code:
<h2>Business Maintenance:</h2>
<form method="post" action="add_Business.asp">
  <table width="800"  border="0" cellspacing="10" cellpadding="0">
    <tr>
      <td width="99">Enter Code:</td>
      <td width="197">Enter Description:</td>
      <td width="131">Enter Unit:</td>
      <td width="145">Upload logo: </td>
      <td width="168">&nbsp;</td>
    </tr>
    <tr>
      <td width="99"><input name="BusinessCode" type="text" size="2" maxlength="2"></td>
      <td width="197"><input name="BusinessDescription" type="text" size="40" maxlength="35"></td>
      <td width="131"><select name="Unit" id="Unit">
        <%
	' Unit
	
	str_SQL = "SELECT * FROM [Unit] ORDER BY [Unit_ID], [Unit Code] ASC;"
	Set obj_RS1 = obj_CN.Execute(str_sql, adBoolean)
		
    ' Build the menu
	While Not obj_RS1.EOF
		
		str_UnitID = obj_RS1("Unit_ID")
		str_Unit = obj_RS1("Unit code")
        str_Option = "<option value=""" & str_UnitID & """#Selected#>" & str_Unit & "</option>"
        Response.Write vbtab & str_Option & vbcrlf
	    obj_RS1.MoveNext
	Wend
%>
      </select></td>
      <td width="145">-----</td>
      <td width="168"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>
</form>
 
I've made that adjustment and I've checked none of the variables contain an ampersand.

Not working just yet.

Thanks.
 
can you put an alert within this section to see if anything appears:

Just want to see if it gets to this point:

Code:
if(xmlhttp.readyState==4 && xmlhttp.status == 200)
  {
[COLOR=red]  
alert("strParams");
  alert(http.responseText);
[/color]
  document.getElementById("divResult").innerHTML = xmlhttp.responseText;
  
  }


--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
Hi,

I added the alert and nothing appears so it doesn't appear to be getting to that point.

Thanks again.
 
This is now working.

Many thanks Vicvirk.

I made a couple of changes:

Code:
function ajaxFunction()
{

var strBusinessCode = document.getElementById("BusinessCode").value;
var strBusinessDescription = document.getElementById("BusinessDescription").value;
var strUnit = document.getElementById("Unit").value;
var strParams = "BusinessCode=" + strBusinessCode;
strParams += "&BusinessDescription=" + strBusinessDescription;
strParams += "&Unit=" + strUnit;

var xmlhttp;
if (window.XMLHttpRequest)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top