dsdt66064367 2017-02-23 08:18
浏览 62
已采纳

如何在jquery中解码/分离php编码的json。

I want to decode my encoded json to jquery and set all separated code to different form element. Here I post my all code.

php code..

<?php
    if(isset($_POST['passid']) === true && empty($_POST['passid'])=== false)
{
    //$q = $_POST['increament'];[![enter image description here][1]][1]
    mysql_connect('localhost','root', '');
    mysql_select_db('kmk_inst');
    $query = mysql_query("select Q_Id, QP_Name, Question from question_paper where Q_Id = '".mysql_real_escape_string(trim($_POST['passid']))."'")
    or die("Wrong Query".mysql_error());
    //echo (mysql_num_rows($query) !== 0) ? mysql_result($query, 0 , 'QP_Name'): 'passid not found';
    //$row = mysql_fetch_array($query);

    if ( mysql_num_rows($query) !== 0 ) {
        $data = json_encode(mysql_fetch_assoc($query));
        echo $data;
    }
?>

HTML CODE

<a class="btn btn-primary col-xs-12 col-lg-2" style="float: right; margin:10px;" id="savenext" name="savenext" onclick="return dataPass()">Save & Next</a></span>
<input type="text" style="width: 410px;text-align: center; margin: 0px;" class="onlyNumber form-control pull-left" id="ques" value="1" name="ques" />
<input type="text" style="width: 410px;text-align: center; margin: 0px;" class="onlyNumber form-control pull-left" id="QPt" value="1" name="QPt" />

** jquery **

$('a#savenext').on('click',function(){
var passid = $('input#ques').val();
//var name = $('input#QPt').val();
if($.trim(passid) != '')
{
        $.post('retrivedata.php', { passid: passid}, function(data){

            //$.each(data, function(i, name) {
            //      alert(name.Q_Id);
            //});
            $('div#datatable').text(data);
            //$('input#QPt').text(QPt);
        });
    }
});

I am new to jquery Ajax and JavaScript so I can`t handle it. any help appreciate.

Currently I am getting result like this.

  • 写回答

3条回答 默认 最新

  • dpbv85276 2017-02-23 08:21
    关注

    Set the proper header in the php

    header('Content-Type: application/json');
    $data = json_encode(mysql_fetch_assoc($query));
    

    jquery will take care of the parsing

    to add the data to your page use append

          $('body').append('<form><p>'+data.Question+'<input value="'+data.QP_Name+'"></from>');
    

    var data = {
      "Q_Id": "1",
      "QP_Name": "test1",
      "Question": "Which is indian capital.?"
    };
    $('#ques').val(data.Question);
    $('#QPt').val(data.QP_Name);
    $('body').append('<form><p>' + data.Question + '<input value="' + data.QP_Name + '"></from>');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a class="btn btn-primary col-xs-12 col-lg-2" style="float: right; margin:10px;" id="savenext" name="savenext" onclick="return dataPass()">Save & Next</a></span>
    <input type="text" style="width: 410px;text-align: center; margin: 0px;" class="onlyNumber form-control pull-left" id="ques" value="1" name="ques" />
    <input type="text" style="width: 410px;text-align: center; margin: 0px;" class="onlyNumber form-control pull-left" id="QPt" value="1" name="QPt" />

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度