Invalid Arguments Passed Implode

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.

Answers


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!


Need Your Help

What is the proper method for reading a GATT characteristic in Android?

bluetooth-lowenergy android-bluetooth android-4.3-jelly-bean

In attempting to read the value of a Bluetooth Low-Energy GATT characteristic in the Android API 18, I came across the following dilemma: What is the proper way to retrieve the value stored in a

Avoiding invalid data statements on conversion to numeric

sas

I have a large table with several variables which will be input to a statistical analysis. As a statistician, I prefer all factors to be numeric, so that they work predictably in regression models,...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.