表单提交后没有发布数据的json_encode

时间:2016-11-29 09:06:48

标签: php html json

我想从这段代码中获取一些输出,但我没有得到我不会得到的东西。

此处代码:

  <?php
       if(isset($_POST['send']) && $_POST['send']=="Send"){
        $array = $_POST['message'];
        $var = call_user_func_array("json_encode",$array);
        $var2 = call_user_func_array("json_encode",array(&$array));
        echo "var => ".$var."\n";
        echo "var2 => ".$var2."\n";
    }
    ?>

输出

var => var2 => "array('a'=>1,'b'=>2)" 

需要

var => 1 var2 => {"a":1,"b":2}

<div class="container">

      <form class="form-horizontal" method="post">
        <div class="form-group">
          <label class="control-label col-sm-2" for="message">Message:</label>
          <div class="col-sm-5">          
            <textarea class="form-control" required id="message" name="message" placeholder="Enter Message">array('a'=>1,'b'=>2)</textarea>
          </div>
        </div>
        <div class="form-group">        
          <div class="col-sm-offset-2 col-sm-10">
            <input type="submit" name="send" class="btn btn-primary btn-lg" value="Send" />
          </div>
        </div>
      </form>
    </div>

提前致谢

1 个答案:

答案 0 :(得分:1)

<?php
if (isset($_POST['send']) && $_POST['send']=="Send") {
$array = $_POST['message'];
$array = eval($array);
$var = "";
if (is_array($array)) {
    $var = json_encode($array);
}
echo "var => ".$var."\n";       
}