开发者

Looking for a way to store the result of a form field with dynamic name and id in an array after submit

开发者 https://www.devze.com 2023-03-12 03:26 出处:网络
I\'m building a form which has the following fields: $k = 1; foreach($ui_fmlSect_pg as $val) { $url_arr[] = $val;

I'm building a form which has the following fields:

$k = 1;
foreach($ui_fmlSect_pg as $val)
{
    $url_arr[] = $val;
    echo '<tr>';
    echo '<td><input size="1" type="text" name="freq_'.$k.'" id="freq_'.$k.'" 
          value="'.$udf_fmlS.'"></td></tr>';
    ++$k;
}

I would like to find a way to add the entered values, that is the content of the variable $udf_fmlS or any manual overwriting, from the *freq_$k* 开发者_StackOverflow中文版-named input fields into an array, but I'm not sure how to proceed with this, especially as the name and id are dynamically generated.

Thanks in advance for your help


You could do something like this:

$freq = array();
foreach ($_POST as $key => $val) {
    // check for submitted data starting with freq_
    if (substr($key, 0, 5) == 'freq_') {
        // use the number after freq_ as the key and the value as the value
        $freq[substr($key, 5)] = $val;
    }
}


If I understand correctly, you will have variables like:

$_REQUEST['freq_0']
$_REQUEST['freq_1']

etc. You could do something like:

<?php
$freq_val_arr = array();
foreach($_REQUEST as $key => $val){
    if (strpos($key, 'freq') === 0){
        $freq_val_arr[] = $val;
    }
}


Just change 'file_'.$k to 'file[]' or to 'file[' . $k . ']'

0

精彩评论

暂无评论...
验证码 换一张
取 消