| | |
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: 45
Reputation:
Solved Threads: 3
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.
Do you see anything that I don't? (Please disregard the fact that I mix British and American english in my code)
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)
<script type="text/javascript" src="js/json2.js"></script> <script type="text/javascript"> var appInc = 0; var rowInc = 0; var On_Error = false; function addRow(r){ var root = r.parentNode;//the root var allRows = root.getElementsByTagName('tr');//the rows' collection var cRow = allRows[0].cloneNode(true)//the clone of the 1st row var cInp = cRow.getElementsByTagName('asp:TextBox');//the inputs' collection of the 1st row for(var i=0;i<cInp.length;i++) {//changes the inputs' names (indexes the names) cInp[i].setAttribute('name',cInp[i].getAttribute('name')+'_'+(allRows.length+1)) } root.appendChild(cRow);//appends the cloned row as a new row } function addAppartement() { appInc++; var appTable = document.getElementById("appTable"); var a = document.createElement('div'); a.setAttribute("id", "a" + appInc); appTable.appendChild(a); var html = '<form name="form' + appInc + '" action="#">\n'; html += '<table>\n'; html += '<tr>\n'; html += '<td class="td" vAlign="top" align="center" width="60"><input id="txtAppNr' + appInc + '" style="width: 44px" type="text"></td>\n'; html += '<td class="td" vAlign="top" align="center" width="50"><input id="txtFloor' + appInc + '" style="width: 44px" type="text"></td>\n'; html += '<td class="td" vAlign="top" align="center" width="50"><input id="txtUnitPlac' + appInc + '" style="width: 43px" type="text"></td>\n'; html += '<td class="td" vAlign="top" align="center" width="115"><input id="txtConsID' + appInc + '" style="width: 104px" type="text"></td>\n'; html += '<td class="td" vAlign="top" align="center" width="90"><input id="txtUnitNr' + appInc + '" style="width: 74px" type="text"></td>\n'; html += '<td class="td" vAlign="top" align="center" width="90"><input id="txtUnitSet' + appInc + '" style="width: 74px" type="text"></td>\n'; html += '<td class="td" vAlign="top" align="center" width="50"><input id="txtFuse' + appInc + '" style="width: 44px" type="text"></td>\n'; html += '<td class="td" vAlign="top" align="center" width="135"><input id="txtCustomer' + appInc + '" style="width: 120px" type="text"></td>\n'; html += '<td class="td" vAlign="top" align="center" width="135"><input id="txtDOB' + appInc + '" style="width: 120px" type="text"></td>\n'; html += '<td class="td" vAlign="top" align="center" width="20"> </td>\n'; html += '</tr>\n'; html += '</table>\n'; html += '</form>'; a.innerHTML = html; document.forms[0].txtNrFlats.value = appInc; } function removeAppartement(appID) { appInc--; var d = document.getElementById('appTable'); var olddiv = document.getElementById('a' + appID); d.removeChild(olddiv); document.forms[0].txtNrFlats.value = appInc; } function SendForm() { var regid = document.getElementById("txtRegistrationID").value; var unitplace; for (i = 0; i < document.forms[0].rdbUnitPlace.length; i++) { if (document.forms[0].rdbUnitPlace[i].checked) { unitplace = document.forms[0].rdbUnitPlace[i].value; } } var nrapps = document.getElementById("txtNrFlats").value; var city = document.getElementById("txtCity").value; var date = document.getElementById("txtDate").value; var installer = document.getElementById("txtInstaller").value; if (nrapps == '' || nrapps == '0') { document.getElementById('response').innerHTML = 'Du måste ange minst en lägenhet för att registreringen ska genomföras'; return; } <hidden>.multihousereg.DoStoringEvent(regid,unitplace,nrapps,city,date,installer,ServerSide_Callback); if (On_Error == true) { return; } var i = 0; var arrSize = Number(nrapps); var arrApps = new Array(arrSize ); for (i = 0; i < arrSize; i++) { arrApps[i] = new Array(9); } var o = objectify(); var jsonString = JSON.stringify(o); alert(jsonString); //<hidden>.multihousereg.PopulateAppartements2(jsonString,ServerSide_Callback2); //window.location = "https://www.a_web_server.se/<folder>/regThanks.aspx?op=multiReg"; } function objectify() { var o = {}; var frms = document.forms; var formID = 1; var id = 1; for (i = 0, maxI = frms.length; i < maxI ; i++) { if (frms[i].name == 'form' + formID) { var frm = frms[i]; var elms = frm.elements; var tmp = {}; for (var j = 0, maxJ = elms.length; j < maxJ; j++) { var el = elms[j]; tmp[el.name] = el.value; } o[frm.name] = tmp; formID++; } } return (o); } function ServerSide_Callback(response) { if (response.error != null) { document.getElementById('response').innerHTML += response.error.value + '<br>'; On_Error = true; return; } } function ServerSide_Callback2(response) { if (response.error != null) { document.getElementById('response').innerHTML += response.error + '<br>'; On_Error = true; return; } } </script>
Do you see anything that I don't? (Please disregard the fact that I mix British and American english in my code)
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.
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.
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.
•
•
Join Date: Jun 2006
Posts: 45
Reputation:
Solved Threads: 3
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:
Am I doing something wrong or am i totally incompetent?
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?
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.
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.
![]() |
Similar Threads
- returning array from ajax.responseText? (JavaScript / DHTML / AJAX)
- AJAX generated <select> and FIREFOX (JavaScript / DHTML / AJAX)
- get html element value using php (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: IE/IP/Javascript problem
- Next Thread: Firefox "Copy Email Address" in IE
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp animate array automatically beta box bug calendar cart checkbox class codes column createrange() css cursor date debugger decimal design dom download dropdown editor element embed enter error explorer firefox focus frameworks getselection google gwt hint html htmlform ie7 ie8 iframe images index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseover onreadystatechange parent paypal php player position post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search select shopping size sql text textarea toggle w3c web website window windowofwords windowsxp wysiwyg \n






