2011-09-08 112 views
0

我想實現在this website.問題的示例

這給表格的例子dynatree嵌入的腳本代碼:

<script type="text/javascript"> 
    $(function(){ 
     $("#tree").dynatree({ 
     checkbox: true, 
     selectMode: 3, 
     onSelect: function(select, node) { 
    // Display list of selected nodes 
    var selNodes = node.tree.getSelectedNodes(); 
    // convert to title/key array 
    var selKeys = $.map(selNodes, function(node){ 
     return "[" + node.data.key + "]: '" + node.data.title + "'"; 
    }); 
    $("#echoSelection4").text(selKeys.join(", ")); 
    }, 


      // In real life we would call a URL on the server like this: 
//   initAjax: { 
//    url: "/getTopLevelNodesAsJson", 
//    data: { mode: "funnyMode" } 
//    }, 
      // .. but here we use a local file instead: 
      initAjax: { 
       url: "sample-data3.json", 
       data: { mode: "funnyMode" } 
       }, 
      onActivate: function(node) { 
       $("#echoActive").text(node.data.title); 
      }, 
      onDeactivate: function(node) { 
       $("#echoActive").text("-"); 
      } 
     }); 
     $("form").submit(function() { 
    // Serialize standard form fields: 
    var formData = $(this).serializeArray(); 

    // then append Dynatree selected 'checkboxes': 
    var tree = $("#tree").dynatree("getTree"); 
    formData = formData.concat(tree.serializeArray()); 

    // and/or add the active node as 'radio button': 
    if(tree.getActiveNode()){ 
    formData.push({name: "activeNode", value: tree.getActiveNode().data.key}); 
    } 

    alert("POSTing this:\n" + jQuery.param(formData)); 

    $.post("<?php echo APP_URL;?>admin/submit_data.php", 
     formData, 
     function(response, textStatus, xhr){ 
     alert("POST returned " + response + ", " + textStatus); 
     } 
); 
    return false; 
}); 


    }); 
</script> 

而且裏面<body>標籤:

<form> 
Username: <input type="text" name="userName" /> 
<br> 
<textarea name="comment"></textarea> 
<br> 
<input type="radio" name="rb1" value="foo" checked> Foo 
<input type="radio" name="rb1" value="bar"> Bar 
<input type="radio" name="rb1" value="baz"> Baz 
<br> 
<input type="checkbox" name="cb1" value="John" checked>John 
<input type="checkbox" name="cb1" value="Paul">Paul 
<input type="checkbox" name="cb1" value="George">George 
<input type="checkbox" name="cb1" value="Ringo">Ringo 
<br> 

<!-- The name attribute is used by tree.serializeArray() --> 
<div id="tree" name="selNodes"> 
</div> 

<input type="submit" value="Send data"> 

而在submit_data.php我哈哈ve

print_r($_POST); 

當我選擇多個節點並單擊發送數據時,數據將發佈到submit_data.php。當我通過螢火蟲檢查控制檯標籤下的帖子參數時,我得到以下數據。

comment test 
rb1 foo 
selNodes restaurant1 
selNodes screen1 
selNodes screen2 
selNodes screen3 
userName gaurav 

print_r($_POST)打印以下數據:

Array 
(
[userName] => gaurav 
[comment] => test 
[rb1] => foo 
[cb1] => John 
[selNodes] => screen3 
) 

理想我應該得到selNodes的所有值。但根據我的理解,因爲參數名稱是相同的所有節點selNodes,這就是爲什麼我只有一個值。

我如何獲得所有值?

+0

我對PHP沒有太多瞭解,但是如果您在選擇多個複選框時遇到同樣的問題('cb1'),這不是dynatree的問題。也許你應該嘗試$ HTTP_POST_VARS [「selNodes」](只是猜測一些谷歌的結果)? – mar10

+0

不好意思,$ HTTP_POST_VARS好像不推薦使用,$ _POST應該是 – mar10

回答

2

我不知道很多關於PHP,但在網絡上的一些複選框樣本追加括號元素名稱,以傳遞數組:

<input type="checkbox" name="cb1[]" value="John" checked>John 
<input type="checkbox" name="cb1[]" value="Paul">Paul 
<input type="checkbox" name="cb1[]" value="George">George 
<input type="checkbox" name="cb1[]" value="Ringo">Ringo 

所以,你可以嘗試

<div id="tree" name="selNodes[]"> 
</div> 
+1

''name =「selNodes []」''在樹的定義中有訣竅。在PHP中,你可以讀取數組的值。非常感謝你 – Lixas

-2

//使用ajax代替提交

$("#tree").dynatree({ 
checkbox: true,      
selectMode: 3, 
      title: "mydyna", 
      fx: { height: "toggle", duration: 200 }, 
      autoFocus: false,  
onSelect: function(select, node) {      
  // Get a list of all selected nodes, and convert to a key array:    
    var selKeys = $.map(node.tree.getSelectedNodes(), function(node){          return node.data.key;        });        
request = $.ajax({ url: "./admin/submit_data.php?getselect=true", type: "post", data: "k="+selKeys.join(", ") }); 
request.done(function (response, textStatus, jqXHR){ $("#ajaxreturn").text(response); }); 
    
  }, 
+0

我試圖修復格式。我不是一個JavaScript專家,但是這味道像不完整的代碼。 –