dongtiannai0654 2016-01-08 15:53
浏览 35
已采纳

将复选框数组分配给变量

I am trying to use a checkbox to populate a single string in a database with specific formatting. I can successfully print the array, but I don't understand how to assign this to a variable in order to connect to the database and to use again elsewhere on the site. Here is the html:

 <input type="checkbox" id="bookCodes" name="bookCodes[]" value="C">Commercials
 <input type="checkbox" id="bookCodes" name="bookCodes[]" value="CP">Commercial Print
 <input type="checkbox" id="bookCodes" name="bookCodes[]" value="V">Voice Overs
 <input type="checkbox" id="bookCodes" name="bookCodes[]" value="X">Background Work
 <input type="checkbox" id="bookCodes" name="bookCodes[]" value="F">Film
 <input type="checkbox" id="bookCodes" name="bookCodes[]" value="TV">Television
 <input type="checkbox" id="bookCodes" name="bookCodes[]" value="BT">Business Theatre Industrials
 <input type="checkbox" id="bookCodes" name="bookCodes[]" value="I">Film Industrials
 <input type="checkbox" id="bookCodes" name="bookCodes[]" value="MV">Music Videos

PHP:

foreach($_POST['bookCodes'] as $codes) {
    print "(".$codes.")";
};

I want the printed string to equal $bookCodes. I have tried putting the array in a function but then I get the first value only. Here is that code:

function prepBookCodes ($value)
{
    if (isset($_POST[$value])) {
        foreach ($_POST[$value] as $codes) {
            return "(" . $codes . ")";
        }
    }
}
$bookCodes = prepBookCodes('bookCodes');

print $bookCodes;

Any help would be greatly appreciated!

  • 写回答

4条回答 默认 最新

  • dongyu8694 2016-01-08 16:19
    关注

    Concatenation is the answer. Make your function return something please!

    function prepBookCodes ($value)
    {
        if (isset($_POST[$value])) {
            foreach ($_POST[$value] as $codes) {
                $string .= "(" . $codes . ")";
            }
        }
    
        return $string;
    }
    $bookCodes = prepBookCodes('bookCodes');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备