【问题标题】:php variable in to JSON objectphp 变量到 JSON 对象
【发布时间】:2016-03-30 15:35:00
【问题描述】:

我在 codeigniter 中这样做

这是我的 php 开关 - 案例部分

case 'check':
$balance = $this->Model_transactions->getUserBalance($this->input->post('userId'));
$needToPay = floatval($this->input->post('tuitionRate')) / 2; // 50%
if ($balance >= $needToPay) {
$results = '{"Result": "OK";"Balance":'.$balance.'}';
}
break;

这是 json 代码

$.ajax({
url: base_url + "api/hires/check/?callback=?",
type: "POST",
dataType: "jsonp",
data: {
userId: $(".navigation").data("login"),
tuitionRate: t.find("#txt-hire-rate").val()
}
}).done(function (a) {
if (a) if ("OK" != a.Result) {
alert (a.Balance);

我想要的是在我的 jQuery 中使用 php $balance 变量。请帮忙。

【问题讨论】:

    标签: php json ajax codeigniter


    【解决方案1】:

    不要尝试构建字符串文字,而是构建一个数组并使用json_encode

    $results = json_encode(array('Result' => 'OK', 'Balance' => $balance), true);
    

    等你准备好了就归还。

    return $results;
    

    【讨论】:

    • 知道了,谢谢!但我如何在 jquery 中使用它。这就是我想要做的。 .done(function (a) { var data = $.parseJSON(a); alert (data.Result);
    【解决方案2】:

    编辑你的 php 脚本

    case 'check':
    $balance = $this->Model_transactions->getUserBalance($this->input->post('userId'));
    $needToPay = floatval($this->input->post('tuitionRate')) / 2; // 50%
    if ($balance >= $needToPay) {
    $data = array(
      'Result' => 'OK',
      'Balance' => $balance 
    );
    echo json_encode($data);
    }
    break;
    

    编辑你的 jQuery 脚本

    $.ajax({
     type: "POST",
     url: '<?php echo base_url()."api/hires/check/?callback=?";?>',
     dataType: 'json',
     data:{userId: $(".navigation").data("login"),
     tuitionRate: t.find("#txt-hire-rate").val()},
     success: function(data){
                if(data.Result != "OK"){
                        alert(data.Balance);
                }
               }
     });
    

    【讨论】:

      【解决方案3】:

      php switch - case部分

          case 'check':
          $balance = $this->Model_transactions->getUserBalance($this->input->post('userId'));
          $needToPay = floatval($this->input->post('tuitionRate')) / 2; // 50%
          if ($balance >= $needToPay) {
          $results = '{"Result": "OK";"Balance":'.$balance.'}';
      
          // add this in your code 
          <script>
             document.getelementbyId('balance').innerHTML=$balance;
          <script>
          }
          break;
          ?>
          <div id='balance' style='display:none;'></div> // added in the code
      

      json 代码

      var bal=$(#balance).html(); // use this variable where you want to
      $.ajax({
      url: base_url + "api/hires/check/?callback=?",
      type: "POST",
      dataType: "jsonp",
      data: {
      userId: $(".navigation").data("login"),
      tuitionRate: t.find("#txt-hire-rate").val()
      }
      }).done(function (a) {
      if (a) if ("OK" != a.Result) {
      alert (a.Balance);
      

      我对php代码进行了修改,并在js中添加了变量,可以在js中的任何地方使用。

      【讨论】:

        猜你喜欢
        • 2012-08-28
        • 2013-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-26
        • 1970-01-01
        • 2018-12-06
        • 2010-11-25
        相关资源
        最近更新 更多