如何在jquery中解码/分离php编码的json? [英] How to decode / separate php encoded json in jquery.?
本文介绍了如何在jquery中解码/分离php编码的json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将编码的json解码为jquery,并将所有分离的代码设置为不同的表单元素.在这里,我发布了所有代码.
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代码.
<?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代码
<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 **
** 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);
});
}
});
我是jquery Ajax和JavaScript的新手,所以我无法处理它.任何帮助表示赞赏.
I am new to jquery Ajax and JavaScript so I can`t handle it. any help appreciate.
目前我正在得到这样的结果.
Currently I am getting result like this.
推荐答案
在php中设置适当的标头
Set the proper header in the php
header('Content-Type: application/json');
$data = json_encode(mysql_fetch_assoc($query));
jquery将负责解析
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" />
这篇关于如何在jquery中解码/分离php编码的json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文