javascript - Why is json_encode returning arrays instead of objects? -
if inclined downvote question, please indicate reason can learn mistakes.
php - edit
<?php require_once "dbconnect.php"; function isempty($str) { return strlen(trim($str)) == 0; } function getwritersdata() { try { if (!isset($_request["userid"]) || isempty($_request["userid"])) { throw new exception('a user-id must supplied.'); } $userid = $_request["userid"]; $dbh = connect2db(); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); $stmt = $dbh->prepare("select title, worktype, formtype, genre, numberofpages, filename, originalfilename writers fkaccounts = :userid"); $stmt->bindparam(':userid', $userid, pdo::param_int); $stmt->execute(); $rows = $stmt->fetchall(); echo json_encode($rows, json_force_object); } catch (pdoexception $e) { echo 'database error: ' . $e->getmessage(); } catch (exception $e) { echo 'general error: ' . $e->getmessage(); } } /** update writers data **/ function savewritersdata() { try { include_once "commonwriterspostelements"; $dbh = connect2db(); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); $stmt = $dbh->prepare("update writers set fkaccounts=:userid, title=:title, worktype=:worktype, formtype=:formtype, genre=:genre, numberofpages=:pages, filename=:filename, originalfilename=:origonal fkaccounts=:userid"); $worktype = "1"; $stmt->bindparam(':userid', $userid, pdo::param_int, 10); $stmt->bindparam(':title', $title, pdo::param_str, 255); $stmt->bindparam(':worktype', $worktype, pdo::param_str, 30); $stmt->bindparam(':formtype', $formtype, pdo::param_str, 30); $stmt->bindparam(':genre', $genre, pdo::param_str, 100); $stmt->bindparam(':pages', $nbrpages, pdo::param_str, 100); $stmt->bindparam(':filename', $newfilename, pdo::param_str, 30); $stmt->bindparam(':original', $file_name, pdo::param_str, 30); $stmt->execute(); } catch (pdoexception $e) { echo 'database error: ' . $e->getmessage(); } catch (exception $e) { echo 'general error: ' . $e->getmessage(); } } function deletewritersdata() { try { if (!isset($_request["userid"]) || isempty($_request["userid"])) throw new exception('user-id missing.'); else { $userid = filter_var(trim($_request["user-id"]), filter_sanitize_string); $userid = htmlspecialchars_decode($userid, ent_quotes); } $dbh = connect2db(); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); $stmt = $dbh->prepare("delete writers fkaccounts=:userid"); $stmt->bindparam(':userid', $userid, pdo::param_int, 10); $stmt->execute(); } catch (pdoexception $e) { echo 'database error: ' . $e->getmessage(); } catch (exception $e) { echo 'general error: ' . $e->getmessage(); } } if (!isset($_request['action']) || isempty($_request['action'])) throw new exception('programmer error: action not posted.'); else { $action = $_request['action']; switch($action) { case 'get-writer-data': getwritersdata(); break; case 'update-writers': select(); break; default: throw new exception("unknown action: " . $action); break; } } if (!isset($_request['action']) || isempty($_request['action'])) throw new exception('programmer error: action not posted.'); else { $action = $_request['action']; switch($action) { case 'get-writer-data': getwritersdata(); break; case 'delete-writers': deletewritersdata(); break; case 'update-writers': savewritersdata(); break; default: throw new exception("unknown action: " . $action); break; } } ?>
js
$(function () { populatewritersdropdowns(); data = {}; data.action = 'get-writer-data'; data.userid = sessionstorage.getitem("user-id"); console.log("user-id-manage-uploads=" + sessionstorage.getitem("user-id")) ajax('post', 'php/manage-uploads.php', data, getsuccess, "error retrieving writer's data: "); $(".tr-clone"); function getsuccess(data) { console.log("data=" + data); var trclone = $(".tr-clone"); var jsondata = $.parsejson(data); var count = 0; (var key in jsondata) count++ $.each(jsondata, function (key, value) { trclone.find(".title").val(value.title); trclone.find(".work-type").val(value.worktype); trclone.find(".form-type").val(value.formtype); trclone.find(".genre").val(value.genre); console.log("value.formtype=" + value.formtype + ", form-type.val()=" + trclone.find(".form-type").val()); console.log("value.genre=" + value.genre + ", genre.val()=" + trclone.find(".genre").val()); trclone.find(".form-type").val(value.formtype); trclone.find(".nbr-pages").val(value.numberofpages); trclone.find(".synopsis a[href='" + value.filename + "']"); if (key === count - 1) return false; trclone = trclone.clone().insertafter($(".tr-clone:last")); }); }
console.log
in js code, json.parse(...) throws error show below.
data=[{"title":"mozart, wunderkind","0":"mozart, wunderkind","worktype":"1","1":"1","formtype":"4","2":"4","genre":"12","3":"12","numberofpages":"250","4":"250","filename":"6532744220.pdf","5":"6532744220.pdf","originalfilename":"mozartwunderkindqueryletter.pd","6":"mozartwunderkindqueryletter.pd"},{"title":"mozart, wunderkind query-letter","0":"mozart, wunderkind query-letter","worktype":"2","1":"2","formtype":"7","2":"7","genre":"9","3":"9","numberofpages":"129","4":"129","filename":"9981287843.pdf","5":"9981287843.pdf","originalfilename":"mozartwunderkindqueryletter.pd","6":"mozartwunderkindqueryletter.pd"}][{"title":"mozart, wunderkind","0":"mozart, wunderkind","worktype":"1","1":"1","formtype":"4","2":"4","genre":"12","3":"12","numberofpages":"250","4":"250","filename":"6532744220.pdf","5":"6532744220.pdf","originalfilename":"mozartwunderkindqueryletter.pd","6":"mozartwunderkindqueryletter.pd"},{"title":"mozart, wunderkind query-letter","0":"mozart, wunderkind query-letter","worktype":"2","1":"2","formtype":"7","2":"7","genre":"9","3":"9","numberofpages":"129","4":"129","filename":"9981287843.pdf","5":"9981287843.pdf","originalfilename":"mozartwunderkindqueryletter.pd","6":"mozartwunderkindqueryletter.pd"}]
vm1161:1 uncaught syntaxerror: unexpected token [ in json @ position 613n.parsejson @ jquery.min.js:4getsuccess @ vm1137:13(anonymous function) @ vm1136:97j @ jquery.min.js:2k.firewith @ jquery.min.js:2x @ jquery.min.js:4(anonymous function) @ jquery.min.js:4
here closely looking @ json string came know echoing string 2 times ... sent client 2 times appended, , why not shown in correct way...
you can use json parser online tool idea json strings .... need figure out in php code, why being sent 2 times, must have echoed twice... if put whole php code more..
so n all, need correct response server, json encoding working fine , encoded string should responded once
bingo !!!! in php code executing switch case twice after line if (!isset($_request['action']) || isempty($_request['action'])) , see below why echoing twice ... , spoils json string
if (!isset($_request['action']) || isempty($_request['action'])) throw new exception('programmer error: action not posted.'); else { $action = $_request['action']; switch($action) { case 'get-writer-data': getwritersdata(); break; case 'update-writers': select(); break; default: throw new exception("unknown action: " . $action); break; } } if (!isset($_request['action']) || isempty($_request['action'])) throw new exception('programmer error: action not posted.'); else { $action = $_request['action']; switch($action) { case 'get-writer-data': getwritersdata(); break; case 'delete-writers': deletewritersdata(); break; case 'update-writers': savewritersdata(); break; default: throw new exception("unknown action: " . $action); break; } }
Comments
Post a Comment