PHP implode(): Invalid arguments passed
Asked Answered
E

3

5

I have a php form which has a checkbox option in which if the user selects 'Other', a text input appears. This is working well but the data is not submitting. Im gettting the error message PHP implode(): Invalid arguments passed

Here is:

PHP validation

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']); 
}

Here is the JS/HTML form

        <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']; ?>"/>
Ezechiel answered 9/11, 2015 at 3:3 Comment(2)
You shouldn't use competitor_model[] on input text. Also, <font> tag has deprecated for many years. Use CSS instead. Last, you should at least check is_array() in PHP before you use implode()Clifford
$_POST['competitor_model'] must be an array. Try var_dump($_POST['competitor_model']) and confirm?Gradin
D
10

$_POST['competitor_model'] is an array.Try this :

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

or try so-

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

PHP implode()

Drice answered 9/11, 2015 at 3:11 Comment(4)
The problem is elsewhere.Clifford
can you just var_dump and see whether the values are got?Drice
may be because $_POST['competitor_model'] is not resulting as an arrayDrice
@ Raptor-yea i didn't notice that it is text,you are right for text we don't initialize it as an array.Drice
G
4

I faced the same problem and this is what worked for me in Laravel:

$contributes = $request->your_contribute;
$each_contribute = implode(',', (array)$contributes);
Germayne answered 11/10, 2017 at 0:18 Comment(0)
B
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.

Banzai answered 14/5, 2018 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.