1

I have a html contact form that has checkboxes. When I go to get the $_POST data all I see for the checkbox field is ARRAY.

Here is the checkbox snippet form the HTML form

  <div class="col-md-3">
    <div class="checkbox">
      <label>
        <input type="checkbox" name="services[]" value="check 1">Type 1
      </label>
    </div>
  </div>

  <div class="col-md-3">
    <div class="checkbox">
      <label>
        <input type="checkbox" name="services[]" value="check 2 ">Type 2
      </label>
    </div>
  </div>

  <div class="col-md-3">
    <div class="checkbox">
      <label>
        <input type="checkbox" name="services[]" value="check 3">Type 3
      </label>
    </div>
  </div>

  <div class="col-md-3">
    <div class="checkbox">
      <label>
        <input type="checkbox" name="services[]" value="check 4">Type 4
      </label>
    </div>
  </div>

and here is my PHP processing file

$email = $_POST['email'];

if ( is_array ( $_POST['services'] ) ) {
    $services = $_POST['services'];
}

echo 'email is: ' . $email . ' services :' . $services;

Email works fine as its just POST data from one input field. But how do I save the array into a variable so it does something like

$services = "check 1, check 2, check 3";
1
  • $_POST['services'] is an array. So you can call each array elements value like $_POST['services'][0], $_POST['services'][1] etc. The easiest way would be to use a foreach loop. Commented Sep 15, 2015 at 16:58

2 Answers 2

3

I guess you are trying to just put that array in your email. In that case PHP will use just word Array to let you know it's actual array, not a string.

To get comma-separated list you should use implode() function on $services (don't forget to filter it first!).

$commaList = implode(', ', $services);
Sign up to request clarification or add additional context in comments.

Comments

0

You should use the implode() function in PHP.

$variable = implode(', ', $services);

You could also use a foreach loop.

$variable = '';
foreach($services as $key => $value){
    $variable .= $value . ', ';
}

rtrim($variable, ', ');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.