Question

I have a form where I'm trying to concatenate the values passed in this area:

<div class='span5' style='margin-left:0px !important;'>
    <label>
        <input type="checkbox" name="arrayValue[]" id="area[0]" value="Sparks"
        style='margin-top:-5px !important;'>Sparks</label>
</div>
<div class='span5' style='margin-left:0px !important;'>
    <label>
        <input type="checkbox" name="arrayValue[]" id="area[1]" value="Stead"
        style='margin-top:-5px !important;'>Stead</label>
</div>
<div class='span5' style='margin-left:0px !important;'>
    <label>
        <input type="checkbox" name="arrayValue[]" id="area[2]" value="North Reno"
        style='margin-top:-5px !important;'>North Reno</label>
</div>

Here is my PHP that's attempting to put the values together...

$arrayValue = array();
$areas = implode("," , $_POST['arrayValue']);

I understand it's probably sloppy, but I can't figure out why I'm getting an invalid arguments error on the implode function.

Was it helpful?

Solution

The error is because the $_POST['arrayValue'] doesn't exist. That will happen if none of the boxes are checked.

Your form is missing the <form> tag and does not have a submit button. Without these elements, it won't work.

Also, you're declaring an array $arrayValue = array(); in your code. I don't think that's necessary here. If you're just trying to get the input values and concatenate them together, you don't need that. As I'm unsure what you're planning to do with it, I've ignored that in my answer.

I've included a working demo below, and that'll probably get you started.


Full code:

<?php
if( isset($_POST['submitButton']) ){
    $areas = implode("," , $_POST['arrayValue']);
    print_r($areas);
}
?>
<form action="" method="post">
<div class='span5' style='margin-left:0px !important;'>
     <label>
         <input type="checkbox" name="arrayValue[]" id="area[0]" value="Sparks" style='margin-top:-5px !important;'> Sparks
     </label>
</div>    

<div class='span5' style='margin-left:0px !important;'>
    <label>
        <input type="checkbox" name="arrayValue[]" id="area[1]" value="Stead" style='margin-top:-5px !important;'> Stead
    </label>
</div>             

<div class='span5' style='margin-left:0px !important;'>
    <label>
        <input type="checkbox" name="arrayValue[]" id="area[2]" value="North Reno" style='margin-top:-5px !important;'> North Reno
    </label>
</div>  
    <input type="submit" name="submitButton"/>

</form>

Output:

Sparks,Stead,North Reno

The above code uses an empty action and thus posts to itself. You'll want to change that according to your requirements.

Hope this helps!

OTHER TIPS

implode will throw a warning if you don't pass an array.

I recommend checking the value is as you expect using var_dump:

var_dump($_POST['arrayValue']);

implode expects an array to passed to the function. What happens when there is no $_POST['arrayValue']? You must ensure that the variable is present:

if(isset($_POST['arrayValue'])){
    $areas = implode("," , $_POST['arrayValue']);
}

Note: I'm not sure where you plan to implement $arrayValue, so it was discarded in this answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top