我想将编码的 json 解码为 jQuery 并将所有分隔的代码设置为不同的表单元素。在这里我发布我的所有代码。
PHP代码:
<?php
if(isset($_POST['passid']) === true && empty($_POST['passid'])=== false)
{
//$q = $_POST['increment'];[![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 代码:
$('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 新手,所以我无法处理它。任何帮助将不胜感激。
目前我得到的结果是这样的。
在 php 中设置正确的标头
header('Content-Type: application/json');
$data = json_encode(mysql_fetch_assoc($query));
jquery 将负责解析
要将数据添加到页面,请使用追加
$('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" />
将 json 设置为 ajax 调用返回的数据类型,如下所示:
$('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);
},'json');
}
});
$('a#savenext').on('click',function(){
var passid = $('input#ques').val();
if($.trim(passid) !=)
{
$.post('retrivedata.php', {passid: passid}, function(data){
var da = JSON.parse(data);
$('div#datatable').text(da.QP_Name);
//alert(da.QP_Name);
});
}
});