如何使用PHP检索json复选框的值

时间:2012-05-01 17:29:32

标签: php json checkbox

我有一个json表单字段,其复选框为编码:

{"name":"Act","description":"Checkthis","type":"checkbox"}

任何人都可以告诉我如何获取此复选框的此值如果已选中使用php

这是表格:

<iframe src='http://www.facebook.com/plugins/registration.php?
                    client_id=360&
                    redirect_uri=http://www.pingcampus.com/facebook_registration_plugin/store_user_data.php&
                    fields=[
                    {"name":"name"},
                    {"name":"email"},
                    {"name":"gender"},
                    {"name":"birthday"},
                    {"name":"captcha"},
                    {"name":"Act","description":"Checkthis","type":"checkbox"}
                    ]'
                    scrolling="auto"
                    frameborder="no"
                    style="border:none"
                    allowTransparency="true"
                    width="500"
                    height="600">
            </iframe>

这是Php代码:

<?php
        define('FACEBOOK_APP_ID', '');
        define('FACEBOOK_SECRET', '');





        // No need to change function body
        function parse_signed_request($signed_request, $secret) {
            list($encoded_sig, $payload) = explode('.', $signed_request, 2);

            // decode the data
            $sig = base64_url_decode($encoded_sig);
            $data = json_decode(base64_url_decode($payload), true);

            if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
                error_log('Unknown algorithm. Expected HMAC-SHA256');
                return null;
            }


            // check sig
            $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
            if ($sig !== $expected_sig) {
                error_log('Bad Signed JSON signature!');
                return null;
            }

            return $data;
        }

        function base64_url_decode($input) {
            return base64_decode(strtr($input, '-_', '+/'));
        }

        if ($_REQUEST) {
            $response = parse_signed_request($_REQUEST['signed_request'],
                            FACEBOOK_SECRET);
            /*
            echo "<pre>";
            print_r($response);
            echo "</pre>"; // Uncomment this for printing the response Array
            */


            echo $_POST["Act"];


            $name = $response["registration"]["name"];
            $email = $response["registration"]["email"];
            $password = $response["registration"]["password"];
            $gender = $response["registration"]["gender"];
            $dob = $response["registration"]["birthday"];
            $phone = $response["registration"]["phone"];
            $act1 = $response["registration"]["Act"];


      // Connecting to Database




            mysql_connect('', '', '') or die(mysql_error());
            mysql_select_db('pingcampus');

            $result = mysql_query("INSERT INTO usxxx set 

            name='$name',
            email='$email',

            gender='$gender',
            act='$act'
             ");
            if ($result) {

                // User successfully stored
                // Redirect to some page
            } else {
                echo mysql_error();

                // Redirect to error page
            }
        } else {
            echo '$_REQUEST is empty';
        }
        ?>

1 个答案:

答案 0 :(得分:1)

<?php
if(isset($_POST["Act"])){
  $act = $_POST["Act"];
  if(!empty($act)){
    // if Act is checked
  }
  else{
    //Act not checked
  }
}
?>

或将value添加到:

{"name":"Act","description":"Checkthis","type":"checkbox", "value":"1"}

并且这样做:

 <?php
    if(isset($_POST["Act"])){
      $act = $_POST["Act"];
      if($act == 1){
        // if Act is checked
      }
      else{
        //Act not checked
      }
    }
    ?>