Pass a 2D-array with AJAX

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pass a 2D-array with AJAX

 
0
  #11
Dec 18th, 2007
There is either a problem with your markup or the way you are using / including Javascript. Did you try the snippet pasted by me in the above post? Did it work for you?
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 68
Reputation: Oxiegen is an unknown quantity at this point 
Solved Threads: 8
Oxiegen Oxiegen is offline Offline
Junior Poster in Training

Re: Pass a 2D-array with AJAX

 
0
  #12
Dec 18th, 2007
I'm sorry. I haven't had the chance to try anything today.
But as soon as I do, I'll post my findings.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 68
Reputation: Oxiegen is an unknown quantity at this point 
Solved Threads: 8
Oxiegen Oxiegen is offline Offline
Junior Poster in Training

Re: Pass a 2D-array with AJAX

 
0
  #13
Jan 7th, 2008
Ok. Now I'm back from the holidays and could check my code. I found that I missed a quote-mark. Now there are no errors, but there still seem to be something odd happening.

In the screenshot, in the attached image, you can see two dynamically created forms marked form1 and form2.
After the calls to the functions objectify and JSON.stringify the alert show the information marked in a green rectangle.
That's not right considering that each form has a number of fields and should be created as an "array".

This is my complete javascript code for adding forms, objectify and such.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript" src="js/json2.js"></script>
  2. <script type="text/javascript">
  3. var appInc = 0;
  4. var rowInc = 0;
  5. var On_Error = false;
  6.  
  7. function addRow(r){
  8. var root = r.parentNode;//the root
  9. var allRows = root.getElementsByTagName('tr');//the rows' collection
  10. var cRow = allRows[0].cloneNode(true)//the clone of the 1st row
  11. var cInp = cRow.getElementsByTagName('asp:TextBox');//the inputs' collection of the 1st row
  12. for(var i=0;i<cInp.length;i++) {//changes the inputs' names (indexes the names)
  13. cInp[i].setAttribute('name',cInp[i].getAttribute('name')+'_'+(allRows.length+1))
  14. }
  15. root.appendChild(cRow);//appends the cloned row as a new row
  16. }
  17.  
  18. function addAppartement() {
  19. appInc++;
  20. var appTable = document.getElementById("appTable");
  21. var a = document.createElement('div');
  22. a.setAttribute("id", "a" + appInc);
  23. appTable.appendChild(a);
  24.  
  25. var html = '<form name="form' + appInc + '" action="#">\n';
  26. html += '<table>\n';
  27. html += '<tr>\n';
  28. html += '<td class="td" vAlign="top" align="center" width="60"><input id="txtAppNr' + appInc + '" style="width: 44px" type="text"></td>\n';
  29. html += '<td class="td" vAlign="top" align="center" width="50"><input id="txtFloor' + appInc + '" style="width: 44px" type="text"></td>\n';
  30. html += '<td class="td" vAlign="top" align="center" width="50"><input id="txtUnitPlac' + appInc + '" style="width: 43px" type="text"></td>\n';
  31. html += '<td class="td" vAlign="top" align="center" width="115"><input id="txtConsID' + appInc + '" style="width: 104px" type="text"></td>\n';
  32. html += '<td class="td" vAlign="top" align="center" width="90"><input id="txtUnitNr' + appInc + '" style="width: 74px" type="text"></td>\n';
  33. html += '<td class="td" vAlign="top" align="center" width="90"><input id="txtUnitSet' + appInc + '" style="width: 74px" type="text"></td>\n';
  34. html += '<td class="td" vAlign="top" align="center" width="50"><input id="txtFuse' + appInc + '" style="width: 44px" type="text"></td>\n';
  35. html += '<td class="td" vAlign="top" align="center" width="135"><input id="txtCustomer' + appInc + '" style="width: 120px" type="text"></td>\n';
  36. html += '<td class="td" vAlign="top" align="center" width="135"><input id="txtDOB' + appInc + '" style="width: 120px" type="text"></td>\n';
  37. html += '<td class="td" vAlign="top" align="center" width="20">&nbsp;</td>\n';
  38. html += '</tr>\n';
  39. html += '</table>\n';
  40. html += '</form>';
  41. a.innerHTML = html;
  42. document.forms[0].txtNrFlats.value = appInc;
  43. }
  44.  
  45. function removeAppartement(appID) {
  46. appInc--;
  47. var d = document.getElementById('appTable');
  48. var olddiv = document.getElementById('a' + appID);
  49. d.removeChild(olddiv);
  50. document.forms[0].txtNrFlats.value = appInc;
  51. }
  52.  
  53. function SendForm() {
  54. var regid = document.getElementById("txtRegistrationID").value;
  55. var unitplace;
  56. for (i = 0; i < document.forms[0].rdbUnitPlace.length; i++) {
  57. if (document.forms[0].rdbUnitPlace[i].checked) {
  58. unitplace = document.forms[0].rdbUnitPlace[i].value;
  59. }
  60. }
  61. var nrapps = document.getElementById("txtNrFlats").value;
  62.  
  63. var city = document.getElementById("txtCity").value;
  64. var date = document.getElementById("txtDate").value;
  65. var installer = document.getElementById("txtInstaller").value;
  66.  
  67. if (nrapps == '' || nrapps == '0') {
  68. document.getElementById('response').innerHTML = 'Du måste ange minst en lägenhet för att registreringen ska genomföras';
  69. return;
  70. }
  71.  
  72. <hidden>.multihousereg.DoStoringEvent(regid,unitplace,nrapps,city,date,installer,ServerSide_Callback);
  73.  
  74. if (On_Error == true) { return; }
  75.  
  76. var i = 0;
  77. var arrSize = Number(nrapps);
  78. var arrApps = new Array(arrSize );
  79. for (i = 0; i < arrSize; i++) {
  80. arrApps[i] = new Array(9);
  81. }
  82.  
  83. var o = objectify();
  84. var jsonString = JSON.stringify(o);
  85. alert(jsonString);
  86. //<hidden>.multihousereg.PopulateAppartements2(jsonString,ServerSide_Callback2);
  87. //window.location = "https://www.a_web_server.se/<folder>/regThanks.aspx?op=multiReg";
  88. }
  89.  
  90. function objectify() {
  91. var o = {};
  92. var frms = document.forms;
  93. var formID = 1;
  94. var id = 1;
  95.  
  96. for (i = 0, maxI = frms.length; i < maxI ; i++) {
  97. if (frms[i].name == 'form' + formID) {
  98. var frm = frms[i];
  99. var elms = frm.elements;
  100. var tmp = {};
  101.  
  102. for (var j = 0, maxJ = elms.length; j < maxJ; j++) {
  103. var el = elms[j];
  104. tmp[el.name] = el.value;
  105. }
  106. o[frm.name] = tmp;
  107. formID++;
  108. }
  109. }
  110. return (o);
  111. }
  112.  
  113. function ServerSide_Callback(response) {
  114. if (response.error != null) {
  115. document.getElementById('response').innerHTML += response.error.value + '<br>';
  116. On_Error = true;
  117. return;
  118. }
  119. }
  120.  
  121. function ServerSide_Callback2(response) {
  122. if (response.error != null) {
  123. document.getElementById('response').innerHTML += response.error + '<br>';
  124. On_Error = true;
  125. return;
  126. }
  127. }
  128. </script>

Do you see anything that I don't? (Please disregard the fact that I mix British and American english in my code)
Attached Thumbnails
screenshot.jpg  
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 68
Reputation: Oxiegen is an unknown quantity at this point 
Solved Threads: 8
Oxiegen Oxiegen is offline Offline
Junior Poster in Training

Re: Pass a 2D-array with AJAX

 
0
  #14
Jan 14th, 2008
Hello? Am I forgotten?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pass a 2D-array with AJAX

 
0
  #15
Jan 14th, 2008
Paste only the relevant and indented code. Sifting through 50+ lines of unindented code is more of a bother.

As far as your problem is concerned, I don't see any name attribute assigned to your form elements and the algorithm I posted uses element names.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 68
Reputation: Oxiegen is an unknown quantity at this point 
Solved Threads: 8
Oxiegen Oxiegen is offline Offline
Junior Poster in Training

Re: Pass a 2D-array with AJAX

 
0
  #16
Jan 15th, 2008
Sorry about that. Won't happen again.

I'll try your solution by adding the name element.
But I was just thinking. If that's the reason why it won't work, then why does the code capture and hold the string in the last form element of each row and not the others?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 68
Reputation: Oxiegen is an unknown quantity at this point 
Solved Threads: 8
Oxiegen Oxiegen is offline Offline
Junior Poster in Training

Re: Pass a 2D-array with AJAX

 
0
  #17
Jan 15th, 2008
Ok. So it works. I now get a correct json string.
Now I'll have to figure out how to Deserialize(?) it in server code-behind into a a use-able .NET object/class/structure.
Any suggestions?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pass a 2D-array with AJAX

 
0
  #18
Jan 15th, 2008
Use any C# JSON binding library which converts from JSON objects to C# objects and vice versa. The bottom section of the JSON home page has a lot of bindings for the C# language. LitJSON is one of those libraries you can use.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 68
Reputation: Oxiegen is an unknown quantity at this point 
Solved Threads: 8
Oxiegen Oxiegen is offline Offline
Junior Poster in Training

Re: Pass a 2D-array with AJAX

 
0
  #19
Jan 17th, 2008
I tried LitJSON only to find out that it's probably compiled using .NET 2.0. I'm working in 1.1.
In my latest attempt I used Jayrock.
And i've also been ripping my hair out trying to get the method JavaScriptDeserializerFromJson in AjaxPro to do my bidding.
No suck luck.

I'm trying to convert the json string into a .NET object in the form of a custom class.

The incoming Json string can contain an array, or not.
But either way I always get an exception thrown in my face.
With Jayrock the error is: "Cannot import System.<whatever> from a JSON Object value".
With AjaxPro there's not always an error but the object is not filled. However, when there is an error it's usually something along the lines of "invalid cast".

Here's two of my attempts:
Dim app As Appartements
'Dim app As String

Try
app = AjaxPro.JavaScriptDeserializer.DeserializeFromJson(jsonString, _
GetType(Appartements)) '//AjaxPro
'app = JsonConvert.Import(GetType(String), jsonString) '//Jayrock
...............

<Serializable()> _
Public Class Appartements
Public strAppNr As String = ""
Public strAppFloor As String = ""
Public strUnitPlace As String = ""
Public strConsID As String = ""
Public strUnitNr As String = ""
Public strUnitSet As String = ""
Public strFuse As String = ""
Public strCustomer As String = ""
Public strDOB As String = ""
End Class
Am I doing something wrong or am i totally incompetent?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pass a 2D-array with AJAX

 
0
  #20
Jan 17th, 2008
> Am I doing something wrong or am i totally incompetent?
You are probably asking this question in the wrong forum. This question has no longer remained a Javascript one since you have been successfully able to send the JSON encoded string to the server.

The way you decode the string on the server is purely dependent on your server side language of choice. Try asking this question in the C# or VB .NET forums of Daniweb.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum


Views: 10406 | Replies: 22
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC