2012-01-11 9 views
0

でAJAXを使用して2次元配列形式の入力を提出:は、次のように私は、フォームを持っているのJavascript

<form name="chat" id="chat" action="" onsubmit="sendMessage()"> 
<a name="chat">&nbsp;</a> 
<input type="text" name="chat" id="chat" size="38" maxlength="50" vertical-align="middle"> 
<input type="hidden" name="action" id="action" value="checkresponse"> 
<input type="hidden" name="response_Array[sessionid]" id="response_Array[sessionid]" value="keu88lh88ini89dgnccob54f27"><br> 
<input type="text" name="response_Array[top]" id="response_Array[top]" value="a"> 
<input type="text" name="response_Array[second]" id="response_Array[second]" value="b"><br> 
<input type="text" name="response_Array[third]" id="response_Array[third]" value="c"><br> 
<input type="text" name="response_Array[fourth]" id="response_Array[fourth]" value="d"><br> 
<input type="text" name="response_Array[input][0]" id="response_Array[input][0]" value="input 0"><br> 
<input type="text" name="response_Array[that][0]" id="response_Array[that][0]" value="that 0"><br> 
<input type="submit" name="submit" id ="submit" value="SEND" style="width: 45px"> 
</form> 

ユーザーがクリックを提出すると、私は、サーバーへのすべての入力フィールドを送信する必要があります。 しかし、ページがリフレッシュされるべきではありません。したがって、私はsendMessage()を作成しました。ここでは、XMLHttpRequestオブジェクトを使用してフォーム送信リクエストを送信しています。

入力フィールドを読み取るために、私は(のsendMessageで使用している):

var infoStr = "chat="+document.forms[0]['chat'].value; 
infoStr += "&action="+document.forms[0]['action'].value; 

私は2次元配列読むべき方法を知りたい:response_Arrayをし、それが通過することができるようにJSON文字列にそれを解析phpサーバコードに渡します。

+0

jQueryとその '.json'メソッドを使用するだけではどうですか? –

答えて

0

それはとても簡単です

var infoStr="" 
for(var i=0;i<document.forms[0].elements.length;i++) 
{ 
    infoStr+=document.forms[0].elements[i].name+"="+document.forms[0].elements[i].value+"&"; 
} 

alert(infoStr); // You will have the full name value pairs. 

が、これはあなたの問題を解決を願っています。

+0

ありがとう..それは働いた。 :) –

+0

あなたはようこそです – AmGates

関連する問題