我想构建一个收集输入值并发送电子邮件的简单表单。一些输入名称是数组,我无法从这些数组中获取值。以下是代码。
$keysToKeep = array('CustomFields[6]', 'email');
$values = array_intersect_key($_POST, array_flip($keysToKeep));
print_r($keysToKeep);
foreach ($values as $name => $value)
{
$message .= $name . ': ' . $value . PHP_EOL;
}
HTML代码:
Your name:
<input type="text" name="CustomFields[6]" id="CustomFields_6_337" value="" fid="4" fname="Name" size='64' maxlength='64'>
Your Email Address:
<input type="text" class="email" name="email" fid="e" fname="Email" ftype="Email" reqd="1"/>
答案 0 :(得分:1)
以PHP格式访问数组值是这样的。
$customField = $_POST['CustomFields'][6];
答案 1 :(得分:0)
以下代码将允许单步执行$ _POST数组并收集它包含的数据。如果$ _POST数组元素中的任何一个是数组本身,则使用另一个循环。
//loop collects form data
foreach($_POST as $key => $field) {
//checks if data being collected is an array
//if it is an array another loop is used to collect data
if(is_array($field)){
foreach($field as $subkey => $subfield){
echo $subfield;
}
}
else{
echo $field;
}
}