PHP implode():传递的参数无效

时间:2015-11-09 03:03:29

标签: php implode

我有一个php表单,其中有一个复选框选项,如果用户选择' Other',则会显示一个文本输入。这很好但数据没有提交。我得到错误消息 PHP implode():传递的参数无效

这是:

PHP验证

if(!isset($_POST['competitor_model'])){   
echo '<p><font color="red" size="+1">• Please select at least one competitor model</font></p>';
} else {
$compmodel = implode(',', $_POST['competitor_model']); 
}

这是JS / HTML表单

        <script type="text/javascript">
        var temp = '';
        function disableTxt() {
        var field = document.getElementById("other");
        if(field.value != '') {
        temp = field.value;
        }
        field.style.display = "none";
        field.value = '';
        }
        function enableTxt() {
        document.getElementById("other").style.display = "inline";
        document.getElementById("other").value = temp;
        }
        </script>         


        <input type="checkbox" value="BMW 3-series" onchange="disableTxt()"  name='competitor_model[]'>BMW 3-series<br>
        <input type="checkbox" value="Mercedes Benz C-class" onchange="disableTxt()"  name='competitor_model[]'>Mercedes Benz C-class<br>
        <input type="checkbox" value="Lexus IS" onchange="disableTxt()"  name='competitor_model[]'>Lexus IS<br>
        <input type="checkbox" value="Audi A4" onchange="disableTxt()" name='competitor_model[]'>Audi A4<br>
        <input type="checkbox" value="Other"  onchange="enableTxt(Number)" name='competitor_model[]'>Other <em>If yes please submit model</em>
        <input type="text" name="competitor_model[]" id="other" style="display:none;" value="<?php if (isset($_POST['competitor_model'])) echo $_POST['competitor_model']; ?>"/>

3 个答案:

答案 0 :(得分:8)

$_POST['competitor_model']是一个数组。试试这个:

if (is_array($_POST['competitor_model']))
        {
        $compmodel = implode(",", $_POST['competitor_model']);
        }

或尝试 -

echo implode(', ', (array)$_POST['competitor_model']);

PHP implode()

答案 1 :(得分:2)

我遇到了同样的问题,这对我有用 在Laravel:

$contributes = $request->your_contribute;
$each_contribute = implode(',', (array)$contributes);

答案 2 :(得分:0)

Error Message :implode(): Invalid arguments passed in C:\xampp\htdocs\test_01\index.php on line 70

Answer : This type of error message occur when implode function do not get any array with string to implode.