943,724 Members | Top Members by Rank

Ad:
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 18th, 2007
0

Re: Pass a 2D-array with AJAX

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?
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 18th, 2007
0

Re: Pass a 2D-array with AJAX

I'm sorry. I haven't had the chance to try anything today.
But as soon as I do, I'll post my findings.
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
652 posts
since Jun 2006
Jan 7th, 2008
0

Re: Pass a 2D-array with AJAX

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
Click image for larger version

Name:	screenshot.jpg
Views:	21
Size:	65.6 KB
ID:	4756  
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
652 posts
since Jun 2006
Jan 14th, 2008
0

Re: Pass a 2D-array with AJAX

Hello? Am I forgotten?
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
652 posts
since Jun 2006
Jan 14th, 2008
0

Re: Pass a 2D-array with AJAX

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jan 15th, 2008
0

Re: Pass a 2D-array with AJAX

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?
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
652 posts
since Jun 2006
Jan 15th, 2008
0

Re: Pass a 2D-array with AJAX

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?
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
652 posts
since Jun 2006
Jan 15th, 2008
0

Re: Pass a 2D-array with AJAX

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jan 17th, 2008
0

Re: Pass a 2D-array with AJAX

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?
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
652 posts
since Jun 2006
Jan 17th, 2008
0

Re: Pass a 2D-array with AJAX

> 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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: IE/IP/Javascript problem
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Firefox "Copy Email Address" in IE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC