2

When a form gets posted, I get some checkbox values like the below:

Array ( [chk0] => true ,
        [chk1] => true,
        [chk3] => true,
        [chk1002] => on,
        [chk1005] => on 
      ) 

Using PHP,How can I construct a JSON request like this by using the above variables?

        "data":
        [
            {
                "checkboxval": true,
                "id": 0
            },
            {
                "checkboxval": true,
                "id": 1
            },
            {
                "checkboxval": true,
                "id": 3
            },
            {
                "checkboxval": true,
                "id": 1002
            },
            {
                "checkboxval": true,
                "id": 1005
            }
        ]

Please note that my POST variables can have other form variables too, but all checkbox values will be named with the prefix "chk"

0

5 Answers 5

1
$output = array();
foreach ($input as $k => $v) {
  $output[] = array(
    'checkboxval' => !!$v,
    'id' => preg_replace('!^chk!', '', $k),
  );
}
header('Content-Type: application/json');
echo json_encode(array('data' => $output));
Sign up to request clarification or add additional context in comments.

3 Comments

How do I get all the posted checkboxes in $input here?
I guess I can use $_POST for $input.. However, $output[] array(...) seems to be syntactically wrong..
@Vincent: It looks like @cletus left out an equals sign. I added it in for him.
1
foreach ($_POST as $k => $v) {
  $output[] = array(
    'checkboxval' => ($v=='on'? true : ($v=='off' ? false : !!$v)),
    'id' => preg_replace('!^chk!', '', $k),
  );
}
header('Content-Type: application/json');
echo json_encode(array('data' => $output));

Credits to cletus who provided the basis for this code.

Comments

0

Have a look at the json_encode() php function. You'll have to massage your array a little to get the exact JSON format you're after.

3 Comments

I am not so much worried for the JSON format..I am interested in the logic for scanning through the dynamically generated checkbox values which have a prefix of chk.
@Vincent Have a look at @Cletus' answer.
@Vincent: I made a minor correction to @cletus' answer. Try it now.
0

Heres an example...

$_POST["chk1"] = "Hello";
$_POST["chk2"] = "World";
$jsonArray = array();
foreach($_POST as $key => $val){
  if(preg_match("/chk/", $key)){
    $jsonArray[$key] = $val;
  }
}
$jsonArray = array("Data" => $jsonArray);
$json = json_encode($jsonArray);
echo "<pre>";
echo $json;
echo "</pre>";

Outputs this:

{"Data":{"chk1":"Hello","chk2":"World"}}

Comments

0

I haven't tested this yet, but maybe something like this:

$json = '"data": [';
$first = true;
foreach ($_POST as $k => $v){
    if (preg_match('/^chk(\d+)$/', $k, $m)){
        if ($first) $first = false; else $json .= ", ";
        $json .= sprintf('{ "checkboxval" : %s, "id" : %s }', ($v && $v != "off") ? "true" : "false", $m[1]);
    }
}
$json .= ']';

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.