0

I'm making a form that allows the user to check where they want certain push notifications to go. There is multiple checkboxes that get POSTed in PHP. Here is my PHP snippet:

if(isset($_POST['Submit'])) {
function sendMessage(){
    $pushmessage = ($_POST['PushMessage']);
    $content = array(
    "en" => $pushmessage
    );

foreach($_POST['formDeliver'] as $check) {
        $segments = $check; 
    }

$fields = array(
  'app_id' => "app-id-here",
  'included_segments' => array($segments),
  'data' => array("foo" => "bar"),
  'contents' => $content
);

Edit: Here is my checkbox code:

      <input class="LCB" type="checkbox" id="district" value="All" name="formDeliver[]" checked><label class="light" for="district">District-Wide (Everyone)</label><br>
      <input class="LCB" type="checkbox" id="high" value="High School" name="formDeliver[]"><label class="light" for="high">High School</label><br>
      <input class="LCB" type="checkbox" id="middle" value="Middle School" name="formDeliver[]"><label class="light" for="middle">Middle School</label><br>
      <input class="LCB" type="checkbox" id="williams" value="Williams Ave." name="formDeliver[]"><label class="light" for="williams">Williams Ave.</label><br>
      <input class="LCB" type="checkbox" id="norwoodv" value="Norwood View" name="formDeliver[]"><label class="light" for="norwoodv">Norwood View</label><br>
      <input class="LCB" type="checkbox" id="sharpsburg" value="Sharpsburg Elementary" name="formDeliver[]"><label class="light" for="sharpsburg">Sharpsburg Ave.</label><br>
      <input class="LCB" type="checkbox" id="athletics" value="Athletics" name="formDeliver[]"><label class="light" for="athletics">Athletics</label>

Whenever the form is submitted, the $fields array submits the message info. I need to pass the segments (values of the checkboxes) as a string like this: "String 1", "String 2", "String 3". Whenever i do this, however, it only grabs the last ticked checkbox and submits it into the $segments variable. Is this something I'm doing wrong or not seeing?

1
  • 1
    You're reassigning the variable $segments each time through the loop. What do you expect it to do instead? Commented May 23, 2016 at 20:31

2 Answers 2

3

Forget the foreach() you are overwriting the $segments variable each time through. Just use the checkbox array:

Based on comments you probably want JSON:

'included_segments' => json_encode($_POST['formDeliver']),

For a comma separated list:

'included_segments' => implode(', ', $_POST['formDeliver']),

In your current code, this would work but is redundant and not needed:

foreach($_POST['formDeliver'] as $check) {
    $segments[] = $check; 
}
$segments = implode(', ', $segments);
//or
$segments = json_encode($segments);
Sign up to request clarification or add additional context in comments.

3 Comments

This works, however the values it is passing now look like this: "All\", \"High School\", \"Middle School", etc. How do i remove the forward slashes?
How would i go about fixing this? I was able to remove the slash marks, however I need my strings to be passed along as an array (Ex. ["String 1", "String 2", "String 3"]). How do i achieve this?
the JSON. When the output is given it shows like this: ["String 1, String 2, String 3"] but i need it to show like this: ["String 1", "String 2", "String 3"], as an array.
0

Since you know the specific names of each checkbox, you can manually create an array that saves TRUE if that specific checkbox was checked or FALSE if it wasn't:

$form_checks = [];//creating an array that will save the info obtained

//saving TRUE or FALSE for each specific checkbox
$form_checks['All'] = (isset($_POST['All']))?TRUE:FALSE;
$form_checks['High School'] = (isset($_POST['High School']))?TRUE:FALSE;
$form_checks['Middle School'] = (isset($_POST['Middle School']))?TRUE:FALSE;
$form_checks['Williams Ave.'] = (isset($_POST['Williams Ave.']))?TRUE:FALSE;
$form_checks['Norwood View'] = (isset($_POST['Norwood View']))?TRUE:FALSE;
$form_checks['Sharpsburg Elementary'] = (isset($_POST['Sharpsburg Elementary']))?TRUE:FALSE;
$form_checks['Athletics'] = (isset($_POST['Athletics']))?TRUE:FALSE;

//inserting the result into your $fields array:
$fields = array(
  'app_id' => "app-id-here",
  'included_segments' => $form_checks,
  'data' => array("foo" => "bar"),
  'contents' => $content
);

If you have an array containing the names of each checkbox, you could also create a foreach loop to do this for you in a similar fashion. Also if you were not looking for TRUE or FALSE specifically, you can just change it for any values you prefer, possibly "CHECKED" and "NOT-CHECKED".

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.