我创建了动态隐藏的元素:
<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 :(得分:0)
正如我所看到的,隐藏字段名称的第一部分不是动态的。我想你应该尝试这样的事情:
foreach($_POST as $key => $value){
if(strpos($key, 'first_part_of_post_array_key') === 0){
//do something with your post value with $value
}
}
我希望它会对你有所帮助。
答案 1 :(得分:0)
在这种情况下隐藏字段使用html元素数组,如下所示,考虑隐藏元素的名称
<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>
将products_of_elements作为数组后,您将获得post_ value为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
}