I am doing a key value pair with a for each loop to get post data, now i would like to catch each element in the rock array below separately so i can assign different functions to them
<input type = "text" name = "rock[bonjovi manson mettalica]">
foreach ($_POST as $key => $value) {
foreach($value as $k => $v){
echo '<p>'.$k.'</p>'; // this echo's all elements in the array, i would like to get each element in the array so i could assign them the below functions
if ($k === "bonjovi"){
//do something
}
if ($k === "manson"){
//do something
}
if ($k === "mettalica"){
//do something
}
}
}
Adding brackets to the Name of an input like rock[]
denotes an array for php when the form is submitted; adding text inside the brackets assigns a name to the element of the array:
<input type="text" name="rock[ a b c ]">
$_POST
array(1) {
["rock"]=>
array(1) {
[" a b c "]=>
string(5) "d e f"
}
}
Maybe instead you want checkboxes? Or other examples here https://secure.php.net/manual/en/faq.html.php#faq.html.select-multiple
<input type="checkbox" name="rock[a]" value="A"> A
<input type="checkbox" name="rock[b]" value ="B"> B
<input type="checkbox" name="rock[c]" value="C"> C
<input type="submit">
$_POST
array(1) {
["rock"]=>
array(2) {
["a"]=>
string(1) "A"
["c"]=>
string(1) "C"
}
}
foreach ($_POST as $key => $value)
$_POST
contains all POST data, in your case you might want to try
foreach ($_POST["rock"] as $key => $value)
to only access the content within 'rock'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.