User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 402,510 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,667 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 5102 | Replies: 22 | Solved
Reply
Join Date: Jun 2006
Location: India
Posts: 6,812
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Pass a 2D-array with AJAX

  #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."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jun 2006
Location: Sweden
Posts: 45
Reputation: Oxiegen is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
Oxiegen Oxiegen is offline Offline
Light Poster

Re: Pass a 2D-array with AJAX

  #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  
Join Date: Jun 2006
Location: Sweden
Posts: 45
Reputation: Oxiegen is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
Oxiegen Oxiegen is offline Offline
Light Poster

Re: Pass a 2D-array with AJAX

  #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.
<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">&nbsp;</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)
Attached Images
File Type: jpg screenshot.jpg (65.6 KB, 8 views)
Reply With Quote  
Join Date: Jun 2006
Location: Sweden
Posts: 45
Reputation: Oxiegen is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
Oxiegen Oxiegen is offline Offline
Light Poster

Re: Pass a 2D-array with AJAX

  #14  
Jan 14th, 2008
Hello? Am I forgotten?
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,812
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Pass a 2D-array with AJAX

  #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."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jun 2006
Location: Sweden
Posts: 45
Reputation: Oxiegen is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
Oxiegen Oxiegen is offline Offline
Light Poster

Re: Pass a 2D-array with AJAX

  #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  
Join Date: Jun 2006
Location: Sweden
Posts: 45
Reputation: Oxiegen is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
Oxiegen Oxiegen is offline Offline
Light Poster

Re: Pass a 2D-array with AJAX

  #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  
Join Date: Jun 2006
Location: India
Posts: 6,812
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Pass a 2D-array with AJAX

  #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."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jun 2006
Location: Sweden
Posts: 45
Reputation: Oxiegen is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
Oxiegen Oxiegen is offline Offline
Light Poster

Re: Pass a 2D-array with AJAX

  #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  
Join Date: Jun 2006
Location: India
Posts: 6,812
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Pass a 2D-array with AJAX

  #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."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

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

All times are GMT -4. The time now is 6:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC