php wordpress $_POST 增量式快捷码

问题描述 投票:0回答:1

也许标题有点刁钻,但我无法用几句话解释清楚我的问题.我使用的是联系表单7。所以基本上在前端我有这样的代码。

<span class="wpcf7-form-control-wrap classmateria materia__1">
<input type="text" name="materia__1" value="" size="40" class="wpcf7-form-control wpcf7-text" aria-invalid="false">
</span>

我正在建立一个函数,从前端的一些字段生成一个xml文件,这个函数是这样的:

add_action( 'wpcf7_before_send_mail', 'CF7_pre_send' );
function CF7_pre_send($cf7) {
$output .= $_POST['materia__1'];
file_put_contents("wp-content/uploads/cf7outputtest.xml", $output);
}

基本上是这样的 $_POST['materia__1']; I'm able to print the output of the above selection for the class "materia__1".Is there a way to achieve that same result by doing something like $_POST['materia__[i]']; for i=0; i++; ? 或者有其他的方法?我的表格可以生成我想要的数量,所以我不能创建$_POST['materia__2'],$_POST['materia__3']等......我不是一个php专家,所以请帮助我的代码:)

非常感谢。

卢卡

php wordpress contact-form-7
1个回答
0
投票

使用数组代替索引名。

在HTML中,我有这样的代码:使用数组代替索引名。name="materia[]" 在PHP中 print_r($_POST[materia]);

价值将以

$_POST['materia'][0], $_POST['materia'][1], etc.

使用 foreach 循环

foreach ($_POST['materia'] as $item) {
    echo $k . ' - ' . $item; // 0 - value, 1 - value, etc.
}
© www.soinside.com 2019 - 2024. All rights reserved.