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 derfloh 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
Joined
Mar 2, 2009
Messages
127
Location
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);
}
 
Thanks for posting the above.

I have tried that but it doesn't update the dropdown automatically.

 
Hi

apex82 said:
it doesn't update the dropdown automatically
Should that affirmation help us with the debugging ? Anyway, two questions for now :
[ul]
[li]Is the ASP code executing ?[/li]
[li]Is the ASP code outputting the last inserted identity value ?[/li]
[/ul]


Feherke.
 
Yes "I think"?? the code is executing and outputting the last value because when I refresh the page manually the record is then added.

Thanks.
 
Hi

apex82 said:
Yes "I think"?? the code is executing and outputting the last value
Think ? What about checking it ? See what exactly is returned to the browser and post it here.
apex82 said:
when I refresh the page manually the record is then added.
[tt]insert[/tt]ing the new data is done before the code I added. So my code may freely crash, the data is already in the database in that moment.


Feherke.
 
Ah I see!

Well in that case nothing is changed in the browser only that the database gets updated.
 
Sorry, I have been a little confused with this.

I have tried this again and Response that is being returned is - failed.

Thanks.
 
Hi

[ul]
[li]Open your page in FireFox.[/li]
[li]Open FireBug.[/li]
[li]In FireBug open the Net tab.[/li]
[li]In the page, fill your [tt]form[/tt] and hit Submit.[/li]
[li]In FireBug click the [+] button in front of the newly appeared POST line.[/li]
[li]In the POST's details open the Response tab.[/li]
[li]Copy & paste here what appears in the Response tab.[/li]
[/ul]


Feherke.
 
This is now updating.

I tried this a couple of times and the numbers 22 the 23 appeared in the response tab in Firebug.

I had a look in the SQL database and in the table these are the company_id numbers for the last records.
 
Hi

In my tests I used the HTML code you posted on 15 Dec 09 9:05 and added the two missing piece ( which you should already have ) :
Code:
[b]<select[/b] [maroon]name[/maroon][teal]=[/teal][green][i]"companydropdown"[/i][/green] [maroon]id[/maroon][teal]=[/teal][green][i]"companydropdown"[/i][/green][b]></select>[/b]
[b]<div[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"divResult"[/i][/green][b]></div>[/b]
The JavaScript code added a new [tt]option[/tt] to the companydropdown [tt]select[/tt] and the word "updated" do the divResult [tt]div[/tt].

Of course, your [tt]select[/tt]'s [tt]id[/tt] may be other, I used companydropdown because we not know what is yours [tt]id[/tt].

Please post your current add_company_function() containing the latest modifications.


Feherke.
 
Here is the code for the function:

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) {
    var newid=parseInt(xmlhttp.responseText,10);
    if (isNaN(newid)) {
      document.getElementById('divResult').innerHTML = 'failed';
    } else {
      var thesel=document.getElementById('companydropdown');
      thesel.options[thesel.options.length]=new Option(document.getElementById('CompanyCode').value,newid);
      document.getElementById('divResult').innerHTML = 'updated';
    }
  }
}

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

Hmm... I use a simple PHP script instead of add_company.asp, just to return a random number ( I have no way to run ASP ) and that JavaScript works just fine.

I have no more idea for now regarding why is not working for you...

Feherke.
 
Okay, thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top