2014-09-29 49 views
0

我動態創建的隱藏要素:如何引用動態帖子變量名稱?

<script type="text/javascript"> 
$('#btnValider').on("click", function() { 
    $('#secteurs_choisis').val(tabSecteurs); // #secteurs_choisis is a hidden element and tabSecteurs is a javascript array 
    for (var s=0; s<tabSecteurs.length; s++) { 
     var sep = "", tmp = ""; 
     for (var p=0; p<tabProduits[s].length; p++) { 
      tmp += sep + tabProduits[s][p].substr(0,tabProduits[s][p].indexOf("|")); 
      sep = ","; 
     } 
     var html = "<input type='hidden' id='products_of_"+tabSecteurs[s]+"' name='products_of_"+tabSecteurs[s]+"' value='"+tmp+"' />"; // here are the dynamic hidden elements 
     $('#produits_choisis').append(html); // #produits_choisis is a div element 
    } 
}); 
</script> 

提交表格後,我想相關的動態隱藏字段的$ _POST變量的值:

$tabSecteurs = explode(',' , $_POST['secteurs_choisis']); 

$tab = array(); 
$tab['id_usermer'] = $_SESSION[CODE_USER]; 
foreach($tabSecteurs as $secteur_code) { 
    $tab['secta_code'] = $secteur_code; 
    $user_secteur->ajouter($tab); 
    $id_user_secteur = $user_secteur->lireDernierId(); 
    $nom_liste_prdt = 'products_of_'.$secteur_code; // here I get the name of the dynamic element 
    $tabProduits = explode(',' , $_POST[$nom_liste_prdt]); // how to get the post data ? 
} 

那麼如何獲得post變量的值?

回答

0

正如我所看到的第一部分隱藏字段名稱不是動態的。我認爲你應該嘗試這樣的事情:

foreach($_POST as $key => $value){ 
    if(strpos($key, 'first_part_of_post_array_key') === 0){ 
     //do something with your post value with $value 
    } 
} 

我希望它能幫助你。在這種情況下隱藏字段的HTML元素的

0

使用數組如下考慮採取products_of_elements作爲數組後隱藏的元素

<script type="text/javascript"> 
$('#btnValider').on("click", function() { 
    $('#secteurs_choisis').val(tabSecteurs); // #secteurs_choisis is a hidden element and tabSecteurs is a javascript array 
    for (var s=0; s<tabSecteurs.length; s++) { 
     var sep = "", tmp = ""; 
     for (var p=0; p<tabProduits[s].length; p++) { 
      tmp += sep + tabProduits[s][p].substr(0,tabProduits[s][p].indexOf("|")); 
      sep = ","; 
     } 
     var html = "<input type='hidden' id='products_of_"+tabSecteurs[s]+"' name='products_of_element[]' value='"+tmp+"' />"; // here are the dynamic hidden elements 
     $('#produits_choisis').append(html); // #produits_choisis is a div element 
    } 
}); 
</script> 

的名字,你會得到崗位價值爲2維陣列,用於products_of_elements

foreach($_POST['products_of_elements'] AS $key=>$value) 
{ 
      echo $key.' => '.$value; //here $key will be index of the element and $value will be posted value for that element 
}