By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

problem in adding and updating at the same time in AJAX

omerbutt
100+
P: 638
AJAX PROB WITH MULTIPLE RECORDS
helo iam having problem in ma code will any body look out an help, i am trying t add sale record in the database and the checkthe quantity of the part slod and decrement it to the current quantity for example if tehre is a part woth part no 10023 and its quantity in show room is 20 and total quantity is 25 then if there is a sale of 2 parts then i have to add the sale and decrement the current quantity in the show room and total quantity also my code works fine till adding but when i open another recordset to decrement teh value of the SHOW ROOM AND TOTAL QUANTITY which is in another table named Stk_items,the xmlobject does not come into the check xmlObjad.status==200 ,
Expand|Select|Wrap|Line Numbers
  1. if(xmlObjad.readystate==4)
  2. {
  3. if(xmlObjad.status==200)
  4. {
  5.  
  6. }
  7. }
  8.  
it do comes iinto the ready state but no response ,i am pasting the working code and non working code also in 2 different parts first i will show you the working part that is simpleadding and yes i am showing you the code for sd2.asp that is the pure asp page because the eror resides there but if neede by any1 i willl paste the AJAX part too
this is the function for the readystate and the add_sale

Expand|Select|Wrap|Line Numbers
  1. function Add_sale(fobj)
  2. {
  3. var str=null;
  4. var vl;
  5.  
  6. vl=VldInputs(fobj);
  7. if(vl==true)
  8. {
  9. str=makestr(fobj,"validate");
  10. if(str!="")
  11. {
  12. xmlObjad=GetXmlHttpObject();
  13.  
  14. xmlObjad.onreadystatechange=StateChangeA;
  15. xmlObjad.open("POST","sd2.asp",true);
  16. xmlObjad.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  17. xmlObjad.send(str);
  18. }
  19. else
  20. {
  21. alert("str is null");
  22. return false;
  23. }
  24. }
  25. else
  26. {
  27. alert(vl);
  28. return false;
  29. }
  30.  
  31. }
  32. function StateChangeA()
  33. {
  34. if(xmlObjad.readyState==4)
  35. {
  36. alert("ready state");
  37. if(xmlObjad.status==200)
  38. {
  39. alert("ready status");
  40. var nod=document.createElement("div");
  41. document.getElementById('result').insertBefore(nod,res2);
  42. nod.innerHTML=xmlObjad.responseText;
  43. var a = xmlObjad.responseText;
  44. alert(a);
  45. }
  46. }
  47. }
  48. function makestr(fobj,valFunc)
  49. {
  50. var str;
  51. str="";
  52. var cmd;
  53. cmd="";
  54. var val;
  55. val="";
  56. for(var i=0; i<=fobj.elements.length-3;i++)
  57. {
  58. switch(fobj.elements.type)
  59. {
  60. case "text":
  61. if(valFunc)
  62. {
  63. cmd= valFunc + "(" + 'fobj.elements.value' + ")";
  64. val = eval('cmd');
  65. }
  66. str+=fobj.elements.name + "=" + fobj.elements.value + "&";
  67. break;
  68.  
  69. case "select-one":
  70. str+=fobj.elements.name + "=" + fobj.elements.options[fobj.elements.selectedIndex].value + "&";
  71. break;
  72. }
  73. }
  74. str = str.substr(0,(str.length - 1));
  75. return str;
  76. function VldInputs(fobj)
  77. {
  78. var str;
  79. str="Please Enter The Following";
  80. str+="\n ***********************";
  81. var flg;
  82. flg=0;
  83. for(var i=0; i<=fobj.elements.length-3;i++)
  84. {
  85. switch(fobj.elements.type)
  86. case "text":
  87. if(fobj.elements.value=="")
  88. {
  89. str+="\n Enter a value for"+fobj.elements.name;
  90. flg=1;
  91. }
  92. break;
  93.  
  94. case "select-one":
  95. if(fobj.elements.options[fobj.elements.selectedIndex].value=="none")
  96. {
  97. str+="\n Enter value for "+fobj.elements.name;
  98. flg=1;
  99. }
  100. break;
  101. }
  102. }
  103. if(flg==0)
  104. {
  105. return true;
  106. }
  107. else
  108. {
  109. return str;
  110. }
  111.  
  112.  
sd2.asp
Expand|Select|Wrap|Line Numbers
  1. <%
  2. response.Expires=-1
  3. billno=request("bill_no")
  4. billdate=request("sel_date")
  5. custname=request("cust_name")
  6. mop=request("m_o_p")
  7. conper=request("con_per")
  8. conph=request("con_no")
  9. codeno=request("code_no")
  10. prtno=request("part")
  11. stkavl=request("stk_avl")
  12. stkreq=request("stk_req")
  13. uprc=request("u_prc")
  14. tprc=request("t_prc")
  15.  
  16. Dim rf, foundit, connf, sqlf
  17. set rf=Server.CreateObject("ADODB.Recordset")
  18. set connf=Server.CreateObject("ADODB.Connection")
  19.  
  20. connf="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db/db1.mdb")
  21. sqlf="select * from Sale_rec;"
  22.  
  23. rf.Open sqlf, connf, 3
  24. foundit=False
  25.  
  26. Do until rf.EOF OR foundit
  27. if (StrComp(rf.Fields("Bill_no"), billno, vbTextcompare) = 0) Then
  28. foundit=True
  29. else
  30. rf.MoveNext
  31. end if
  32.  
  33. loop
  34.  
  35. rf.Close
  36. set rf = Nothing
  37. set connf=Nothing
  38.  
  39. if foundit=False Then
  40. /*THIS BOLD PART PRODUCES EROR IF YOU REMOVE,IT WORKS FINE, ADDS AND GIVES RPOPER RESPON 
  41. set r=Server.CreateObject("ADODB.Recordset")
  42. set conn=Server.CreateObject("ADODB.recordset") 
  43. conn="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db/db1.mdb")
  44. SQLQ="select * from Stk_items where Stk_items.Prt_no='"& prtno &"';"
  45.  
  46. r.CursorType=2
  47. r.LockType=3
  48. r.Open SQLQ, conn, 3
  49. r.Fields("Sroom")=stkrem
  50. Cint(stkremq)=r.Fields("Qtty")
  51. stkrem=stkavl-stkreq
  52. r.Fields("Qtty")=stkremq
  53. r.Update
  54. r.Close
  55. sql="SELECT * from Sale_rec;"
  56. r.Cursortype = 2
  57. r.LockType = 3
  58.  
  59. r.Open sql, conn, 3
  60. r.AddNew
  61.  
  62. r.Fields("Cust_name")=custname
  63. r.Fields("Mop")=mop
  64. r.Fields("Con_per")=conper
  65. r.Fields("Con_no")=conph
  66. r.Fields("Code_no")=codeno
  67. r.Fields("Prt_no")=prtno
  68. r.Fields("Stk_avl")=stkavl
  69. r.Fields("Stk_req")=stkreq
  70. r.Fields("U_prc")=uprc
  71. r.Fields("T_prc")=tprc
  72.  
  73. r.Update
  74. r.close
  75.  
  76. set r = Nothing
  77. set conn = Nothing 
  78. response.Write("Value Successfully Added")
  79. else
  80. response.Write("The BILL-NO already exists in the database")
  81. end if
  82. response.End()
  83.  
Dec 20 '07 #1
Share this Question
Share on Google+
18 Replies


acoder
Expert Mod 15k+
P: 16,027
What I would suggest is that you get the ASP working first and then come back so we can sort out any possible Ajax problems.
Dec 22 '07 #2

omerbutt
100+
P: 638
hey the asp is perfect ya know when i tried to run it without using xmlobject means by adding action="sd2.asp" in the <form tag> it rnus perfect but using xmlobject it makes some kind of a mes in the asp code i donno its because of the values comming onto the asp page or some what........
What I would suggest is that you get the ASP working first and then come back so we can sort out any possible Ajax problems.
Dec 22 '07 #3

acoder
Expert Mod 15k+
P: 16,027
OK, can I see what this line:
str=makestr(fobj,"validate");
produces?
Dec 22 '07 #4

omerbutt
100+
P: 638
its calling the function makestr(fobj,validate) which returns a concatinated string consisting of the values entered in the fields which on returning is stored into str and then it is validated and sent to the page using post method with send(str);
here is the code for the funtion amkestr()
Expand|Select|Wrap|Line Numbers
  1. function makestr(fobj,valFunc)
  2. {
  3. var str;
  4. str="";
  5. var cmd;
  6. cmd="";
  7. var val;
  8. val="";
  9. for(var i=0; i<=fobj.elements.length-3;i++)
  10. {
  11. switch(fobj.elements.type)
  12. {
  13. case "text":
  14. if(valFunc)
  15. {
  16. cmd= valFunc + "(" + 'fobj.elements.value' + ")";
  17. val = eval('cmd');
  18. }
  19. str+=fobj.elements.name + "=" + fobj.elements.value + "&";
  20. break;
  21.  
  22. case "select-one":
  23. str+=fobj.elements.name + "=" + fobj.elements.options[fobj.elements.selectedIndex].value + "&";
  24. break;
  25. }
  26. }
  27. str = str.substr(0,(str.length - 1));
  28. return str;
  29.  
  30.  
OK, can I see what this line:
str=makestr(fobj,"validate");
produces?
Dec 22 '07 #5

acoder
Expert Mod 15k+
P: 16,027
its calling the function makestr(fobj,validate) which returns a concatinated string consisting of the values entered in the fields which on returning is stored into str and then it is validated and sent to the page using post method with send(str);
I knew that, but with the absence of the form, I just wanted an example of a string that that line produced. Anyway, fobj.elements is an array, so you should index it properly with elements[i].type, elements[i].value, etc.
Dec 22 '07 #6

omerbutt
100+
P: 638
an example concatinated string returned wil be like this that wil be posted to the asp page where i am requesting the same values
"bill_no=1001&sel_date=13/2/2007&cust_name=OmerAslam&....and so on till the last input "
i hope you are getting it and that suggestion of your about that array thing i will do that too but that is not the issue...
regards,
omer.

I knew that, but with the absence of the form, I just wanted an example of a string that that line produced. Anyway, fobj.elements is an array, so you should index it properly with elements[i].type, elements[i].value, etc.
Dec 24 '07 #7

acoder
Expert Mod 15k+
P: 16,027
If you alert the str after setting it with makestr, does it show up as you expect it to?
Dec 24 '07 #8

omerbutt
100+
P: 638
yes i am getting the alert aith the desired vales,its just that when i use xmlobj for the submission of the form it creates the problem :( , normal form submission doesnt do that stupid thing
If you alert the str after setting it with makestr, does it show up as you expect it to?
Dec 24 '07 #9

acoder
Expert Mod 15k+
P: 16,027
Use encodeURIComponent on each value being passed. This ensures that the values are encoded properly.
Dec 24 '07 #10

omerbutt
100+
P: 638
i dont know aboutthat stuff can you guide me a little bit ......but if this would have been a problem why it is adding the values in the other sales table, why it makes a mess when i try to update in another table along with adding the sales
:(..........?
Use encodeURIComponent on each value being passed. This ensures that the values are encoded properly.
Dec 24 '07 #11

acoder
Expert Mod 15k+
P: 16,027
i dont know aboutthat stuff can you guide me a little bit ......but if this would have been a problem why it is adding the values in the other sales table, why it makes a mess when i try to update in another table along with adding the sales
That's true. What is the actual status instead of 200?
Dec 24 '07 #12

omerbutt
100+
P: 638
didnt knew about it....but after your advice i just tried to remove that part in the statechanged function means the inner check i.e
if(xmlobjad.status==200)
{
}
then it gave me error regarding sd2.asp that [b]either bof or eof is true or the current record does not exist whereas the value is there in the table :(
That's true. What is the actual status instead of 200?
Dec 25 '07 #13

omerbutt
100+
P: 638
hey guess what ...in the function where i am trying to get the values,concatinate them and store them into the str i placed alert for each of the names of the inuts and the values and there i found that it is cgetting all the values from the form instead of the partno which is a drop down selection menu that is being populated dynamically after selecting the code_no , :D atleast i got to the root of the problem, it is getting the other values for the drop down selection menus but they are not populated dynamically,now the code for the form inputs code_no and part is
[html]
<tr>
<td class="add"><div align="left">Code-No:</div></td>
<td width="150" align="Center" valign="Center"><div align="left">
<SELECT name="code_no" onchange="getddl(this);" style="width:194px; height:17px; font-size:11px; font-family:tahoma; border-style:solid; border-width:1px; border-color:#4C4A48; color:#00FF00; background-color:#0A0501 ">
<option value="none" selected="selected">Please Select A Code-No</OPTION>
<%strSQL="Select * From Code_part Order by Code_part.Code_no;"
r.Open strSQL, connstr, 3
while not r.EOF%>
<option value="<%=r.Fields("Code_no")%>"><%=Response.Write (r.Fields("Code_no"))%></option>
<%r.MoveNext
wend%>
<%r.Close%>
</SELECT>
</div></td>
</tr>
<tr>
<td class="add"><div align="left">Part number:</div></td>
<td width="150" align="Center" valign="Center">
<div align="left">
<SELECT name="part" onchange="getqtty(this)" style="width:194px; height:20px; font-size:11px; font-family:tahoma; border-style:solid; border-width:1px; border-color:#4C4A48; color:#00FF00; background-color:#0A0501 ">
<option value="none" selected="selected">Select Part No</option>
</SELECT>
</div></td>
</tr>
[/html]

the code for the 2 funtions called in these two dropdown menus namely:
getddl(this), and getqtty(this) id here
Expand|Select|Wrap|Line Numbers
  1.  
  2. function getddl(dd1)
  3. {
  4.  var fobj;
  5.  fobj=document.forms["salefrm"];
  6.   if(fobj.elements['code_no'].options[fobj.elements['code_no'].selectedIndex].value=="none")
  7.   {
  8. clrprts();
  9. clrstkavl();
  10. clruprc();
  11. clrstkreq();
  12. clrtprc();
  13. enble();
  14.  }
  15.   else
  16.    {
  17.   document.getElementById('stk_avl').value="";
  18.   clruprc();
  19.   clrstkreq();
  20.   clrtprc();
  21.   enble();
  22.   var idx = dd1.selectedIndex;
  23.   var val = dd1[idx].text;
  24.   var par = document.forms["salefrm"];
  25.   var parelmts = par.elements;
  26.   var partsel = parelmts["part"];
  27.   var code = val;
  28.  
  29. xmlHttp=GetXmlHttpObject();
  30.  
  31.   if(xmlHttp==null)
  32.   {
  33.    alert("Your Browser is not supported");
  34. return;
  35.   }
  36.   else
  37.   {
  38.    xmlHttp.onreadystatechange=StateChangedP;
  39.    xmlHttp.open("GET","Get_prt.asp"+"?"+"code="+val,true);
  40.    xmlHttp.send(null);
  41.    }
  42.    }
  43. }
  44. function StateChangedP()
  45. {
  46. if(xmlHttp.readystate==4)    
  47. {
  48. if (xmlHttp.status==200) 
  49. {
  50. var str=null;
  51. str=xmlHttp.responseText;
  52. var vald=fillParts(str);
  53. if(vald==false)
  54. {
  55. alert("fail");
  56. }
  57. }
  58. else
  59. {
  60. alert(xmlHttp);
  61. alert("ERROR Occurred");
  62. return false;
  63. }
  64. }
  65. }
  66. function getqtty(ddlp)
  67. {
  68. var fobj;
  69. fobj=document.forms["salefrm"];
  70. if(fobj.elements['part'].options[fobj.elements['part'].selectedIndex].value=="none")
  71. {
  72. clrstkavl();
  73. clruprc();
  74. clrstkreq();
  75. clrtprc();
  76. enble();
  77. }
  78. else
  79. {
  80. var idx=ddlp.selectedIndex;
  81. var val=ddlp[idx].text;
  82. xmlObj=GetXmlHttpObject();
  83.  
  84. if(xmlObj==null)
  85.     {
  86.     alert("Your browser isnt supported");
  87.     return;
  88.     }
  89. else
  90.     {
  91.     xmlObj.onreadystatechange=StateChangedQ;
  92.     xmlObj.open("GET","Get_qtty.asp"+"?"+"part="+ val,true);
  93.     xmlObj.send(null);
  94.     }
  95. }
  96. }
  97. function StateChangedQ()
  98. {
  99. if(xmlObj.readystate==4)
  100. {
  101. if(xmlObj.status==200)
  102. {
  103. document.getElementById('stk_avl').value=xmlObj.responseText;
  104. }
  105. else
  106. {
  107.     alert("not Ready State");
  108.     clrstkavl();
  109.     return false;
  110. }
  111. }
  112. }
  113.  
so any suggestions regarding the new discovery of the problem is it because the drop-down selection menu is created dynamically and i should get the values by som other method or any check inthe getformvalues function
waiting for reply
regards,
omer
That's true. What is the actual status instead of 200?
Dec 25 '07 #14

acoder
Expert Mod 15k+
P: 16,027
What does fillParts do? Can you show the code for that?

Also, check for any errors.
Dec 26 '07 #15

omerbutt
100+
P: 638
yeah sure i show you the code and the fillparts() function fills the dropdown selection menu with those part numbers which have the code-no matching to the one selected in the "code_no" drop down selection menu in the database, means the drop down menu whichis created dynamically. the code is here, so far i havnt found any erorrs in it, it is giving the exact parts and exact number of parts in the selection menu, but one thing i suspect tha it is using fobj.elements.text instead of using value
Expand|Select|Wrap|Line Numbers
  1. function fillParts(str)
  2. {
  3.   var fobj = document.forms["salefrm"];
  4.   var fobjelement = fobj.elements;
  5.   var partddlsel = fobjelement["part"];
  6.  
  7.   var prtnostr = str;
  8.   var partar = prtnostr.split("|");
  9.  
  10.   partddlsel.length = 1;
  11.   partddlsel.length = partar.length;
  12.  
  13.       for (var o=1; o < partddlsel.length; o++)
  14.        {
  15.              partddlsel[o].text = partar[o];
  16.        }
  17. return true;
  18. }
  19.  
any suggesstions?....
regards,
omer
What does fillParts do? Can you show the code for that?

Also, check for any errors.
Dec 29 '07 #16

acoder
Expert Mod 15k+
P: 16,027
You need to set the value too. See this article for an explanation of dynamically creating options.
Dec 29 '07 #17

omerbutt
100+
P: 638
hey thanks alot buddy that did it :) this thread is closed
regards,
omer.
You need to set the value too. See this article for an explanation of dynamically creating options.
Jan 10 '08 #18

acoder
Expert Mod 15k+
P: 16,027
Finally! In the end, a simple problem/solution, eh? Glad you managed to solve it.
Jan 10 '08 #19

Post your reply

Sign in to post your reply or Sign up for a free account.