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

Page/Database isn't always updated.

Status
Not open for further replies.

apex82

Programmer
Mar 2, 2009
127
GB
I am using AJAX to insert, update and delete to and from a database.

My problem is it doesn't always work sometimes it does and sometimes it doesn't.

When I just post the form data my database is always updated so there no problem at that end.

Here is my AJAX, can anyone help me?

Code:
function add_company_function()
{

var strCompanyCode = document.getElementById("CompanyCode").value;
var strCompanyDescription = document.getElementById("CompanyDescription").value;
var strModule = document.getElementById("Module").value;
var strLogoID = document.getElementById("LogoID").value;
var strParams = "CompanyCode=" + strCompanyCode;
strParams += "&CompanyDescription=" + strCompanyDescription;
strParams += "&Module=" + strModule;
strParams += "&LogoID=" + strLogoID;

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 && xmlhttp.status == 200)
  {
	  
  document.getElementById("divResult").innerHTML = xmlhttp.responseText;
  
  }
}

xmlhttp.open("POST","add_company.asp",true);

xmlhttp.setRequestHeader("Content-type", "application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]
xmlhttp.setRequestHeader("Content-length", strParams.length);
xmlhttp.setRequestHeader("Connection", "close");

xmlhttp.send(strParams);
}
 
Hi

Other then not [tt]escape()[/tt]ing the values in the POSTed data, there is no obvious error.

Please show us the related HTML and the code where add_company_function() is called.

Feherke.
 
Okay here is the form where the function is called:

Code:
<form name="add">
  <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 Module:</td>
      <td width="145">logo ID:</td>
      <td width="168"> </td>
    </tr>
    <tr>
      <td width="99"><input name="CompanyCode" id="CompanyCode" type="text" size="2" maxlength="2"></td>
      <td width="197"><input name="CompanyDescription" type="text" id="CompanyDescription" size="40" maxlength="35"></td>
      <td width="131"><select name="Module" id="Module">
        <%
    ' Select Module
    
    str_SQL = "SELECT * FROM [Module] ORDER BY [Module_ID], [Module Code] ASC;"
    Set obj_RS1 = obj_CN.Execute(str_sql, adBoolean)
        
    ' Build the menu
    While Not obj_RS1.EOF
        
        str_ModuleID = obj_RS1("Module_ID")
        str_Module = obj_RS1("Module code")
        str_Option = "<option value=""" & str_ModuleID & """#Selected#>" & str_Module & "</option>"
        Response.Write vbtab & str_Option & vbcrlf
        obj_RS1.MoveNext
    Wend
%>
      </select></td>
      <td width="145"><input name="LogoID" id="LogoID" type="text" size="8" ></td>
      <td width="168"><input type="submit" name="Submit" value="Submit" [COLOR=red]onclick="add_company_function();"[/color] ></td>
    </tr>
  </table>
</form>


And here is the add_company.asp page:

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

str_SQL = "INSERT INTO Company ([Company Code], [ModuleID], [Company Description], [LogoID])" 
str_SQL = str_SQL & " VALUES "
str_SQL =str_SQL & "('" & Request.Form("CompanyCode") & "', '" & Request.Form("Module") & "', '" & Request.Form("CompanyDescription") & "', '" & Request.Form("LogoID") & "')"

Set obj_RS1 = obj_CN.Execute(str_sql, adBoolean)

%>
<%
Response.Write("updated")
%>
 
I've figured that it works all the time when I add ;return false;" to where it calls the function:

Code:
 onclick="add_company_function();return false;"

The only problem then is it doesn't update the page, with the new record.

Is there anyway to do both so I can update the page too?

Thanks.
 
I don't want to refresh the whole page.

I list a group of records and when I add a record as in the code above I just want the list to be updated.

When I didn't have the ;return false;" the list updated but not all the time.

Is there no way to do both?

Thanks.
 
I've had a look for similar examples but can't seem to come across anything.

Is it possible to update a list or dropdown box on the page after modifying/inserting a new record?

Thanks.
 
Hi

apex82 said:
Is it possible to update a list or dropdown box on the page after modifying/inserting a new record?
Certainly. Otherwise AJAX would not be helpful.

Just replace that [tt]Response.Write("updated")[/tt] to return something more useful, either :
[ul]
[li]HTML document fragment to be added to the document[/li]
[li]data to be used by JavaScript functions to update the document[/li]
[/ul]
Personally I prefer the second way.


Feherke.
 
Okay thanks for your direction feherke.

Trouble is I still can't think of what i need to do next to replace the Response.Write.

Before I added ();return false;" the lists and dropdown automatically updated without refreshing anything - albeit not all the time. Do I need to just somehow reverse the ();return false;"?

Thanks.
 
Hi

apex82 said:
Before I added ();return false;" the lists and dropdown automatically updated without refreshing anything - albeit not all the time.
That was an unpredictable mess. Anyway, I still not believe you that you submitted the [tt]form[/tt] and it worked "without refreshing anything". Or we are just misunderstanding each other.
apex82 said:
Do I need to just somehow reverse the ();return false;"?
No. You need to go forward this way : let the AJAX do the whole job.
apex82 said:
Trouble is I still can't think of what i need to do next to replace the Response.Write.
Well, is better to add to the document what the ASP returns, to ensure that the validated/formatted/processed data is displayed. But essentially that would be the same as you sent to the ASP. In most of the cases you can just "cheat" and add the [tt]form[/tt] data to the document.

Probably anything would be cleaner than my explanation. Do you have an URL to a publicly accessible copy of that page ? Would be easier to give instructions if I see it in live in FireBug.

Feherke.
 
Okay thanks. I'll have a think of how i can do that.

Unfortunately I haven't got a public URL that I can show you.

 
Hi feherke,

I can't seem to get any further on this.

From the code in my first couple of posts, would you be able to point me in the direction of what i need to in order to update the page?

Thanks.
 
Hi

For now I am not sure what you want. Based on your HTML fragment, your [tt]form[/tt] looks like this :

[tt]+-------------+--------------------+---------------+----------+------------+
| Enter Code: | Enter Description: | Enter Module: | logo ID: | |
+-------------+--------------------+---------------+----------+------------+
| [_________] | [________________] | [_________[v] | [______] | [>Submit<] |
+-------------+--------------------+---------------+----------+------------+[/tt]

First I thought that if you fill it like this :

[tt]+-------------+--------------------+---------------+----------+------------+
| Enter Code: | Enter Description: | Enter Module: | logo ID: | |
+-------------+--------------------+---------------+----------+------------+
| [[red]one[/red]______] | [[red]two[/red]_____________] | [[red]three[/red]____[v] | [[red]four[/red]__] | [>Submit<] |
+-------------+--------------------+---------------+----------+------------+[/tt]

After pressing Submit the table should look like this :

[tt]+-------------+--------------------+---------------+----------+------------+
| Enter Code: | Enter Description: | Enter Module: | logo ID: | |
+-------------+--------------------+---------------+----------+------------+
| [red]one[/red] | [red]two[/red] | [red]three[/red] | [red]four[/red] | [>Delete<] |
+-------------+--------------------+---------------+----------+------------+
| [_________] | [________________] | [_________[v] | [______] | [>Submit<] |
+-------------+--------------------+---------------+----------+------------+[/tt]

But then you mentioned "the lists and dropdown automatically updated". Now I am confused. With what should the dropdown be updated ?


Feherke.
 
I basically display dropdown boxes on the page, these are for the user to choose a company to delete or edit once they have been added.

The dropdown contain the information regarding the company. i.e the company code.

Thanks.
 
I have moved the return false out of the main page into the ajax page after here:
Code:
if(xmlhttp.readyState==4 && xmlhttp.status == 200)
  {

and this is now producing the result I want where the page is being updated.

Thanks.
 
I have moved the return false out of the main page into the ajax page after here:
CODE
if(xmlhttp.readyState==4 && xmlhttp.status == 200)
{

and this is now producing the result I want where the page is being updated.

Thanks.

This was a false alarm. It is still not always updating.
 
Hi

Ah, so there is another dropdown, not included in you previously posted code, which contains data from table Company.

I suppose the identity/serial/auto_increment value assigned by the database server to the record is needed in that dropdown, so the mentioned "cheating" is not a way.

What database server are you using ? You have an identity/serial/auto_increment field in the Company table, right ? And you are using it when populating that dropdown, right ?

Feherke.
 
What database server are you using ?

SQL Server 2008

You have an identity/serial/auto_increment field in the Company table, right ? And you are using it when populating that dropdown, right ?

Yes, that is right.
 
Hi

Probably something like this will do it :
Code:
<!-- #include file="_ini/inc_params.asp" -->
<!-- #include file="_ini/inc_opendb.asp" -->
<%

str_SQL = "INSERT INTO Company ([Company Code], [ModuleID], [Company Description], [LogoID])"
str_SQL = str_SQL & " VALUES "
str_SQL =str_SQL & "('" & Request.Form("CompanyCode") & "', '" & Request.Form("Module") & "', '" & Request.Form("CompanyDescription") & "', '" & Request.Form("LogoID") & "')"

Set obj_RS1 = obj_CN.Execute(str_sql, adBoolean)

[highlight]str_SQL = "SELECT @@IDENTITY AS LASTID"[/highlight]
[highlight]Set obj_RS2 = obj_CN.Execute(str_SQL, adCmdText)[/highlight]
[highlight]row = obj_RS2.GetRows(1)[/highlight]

%>
<%
Response.Write([highlight]row(0,0)[/highlight])
%>
Code:
xmlhttp[teal].[/teal]onreadystatechange[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
  [b]if[/b] [teal]([/teal]xmlhttp[teal].[/teal]readyState[teal]==[/teal][purple]4[/purple] [teal]&&[/teal] xmlhttp[teal].[/teal]status[teal]==[/teal][purple]200[/purple][teal])[/teal] [teal]{[/teal]
    [b]var[/b] newid[teal]=[/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]xmlhttp[teal].[/teal]responseText[teal],[/teal][purple]10[/purple][teal]);[/teal]
    [b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]isNaN[/color][teal]([/teal]newid[teal]))[/teal] [teal]{[/teal]
      document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'divResult'[/i][/green][teal]).[/teal]innerHTML [teal]=[/teal] [green][i]'failed'[/i][/green][teal];[/teal]
    [teal]}[/teal] [b]else[/b] [teal]{[/teal]
      [b]var[/b] thesel[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'companydropdown'[/i][/green][teal]);[/teal]
      thesel[teal].[/teal]options[teal][[/teal]thesel[teal].[/teal]options[teal].[/teal]length[teal]]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Option[/color][teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'CompanyCode'[/i][/green][teal]).[/teal]value[teal],[/teal]newid[teal]);[/teal]
      document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'divResult'[/i][/green][teal]).[/teal]innerHTML [teal]=[/teal] [green][i]'updated'[/i][/green][teal];[/teal]
    [teal]}[/teal]
  [teal]}[/teal]
[teal]}[/teal]
Note that I know nothing about ASP, so my code may contain bugs.

In the JavaScript code I supposed that the dropdown's [tt]id[/tt] is companydropdown.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top