javascript - Getting Pre-Populated Tags Via JQuery/AJAX -
i'm using "bootstrap-tags" jquery plugin (https://github.com/maxwells/bootstrap-tags) add simple tagging interface web app i'm working on. in happy path, can plugin work fine, need pull tags shown on page load mysql db. code have is:
$.get("getcurrenttags.php", {"id": id] }, function(data) { $('#my-tag-list').tags({ tagdata: data, bootstrapversion: "2" }); } );
doing gives me following error in chrome console:
uncaught typeerror: cannot use 'in' operator search '12' in ["civil war"]
i think more of general problem , has how 'data' formatted going callback function, especially since works if manually type like
tagdata: ["civil war"]
but can't figure out exactly.
for thoroughness, 'getcurrenttags.php' looks this:
include('db.php'); $oid = $_get['id']; $currenttags = array(); $currenttagsquery = mysql_query("select tag_name t_tag t join t_object_tag ot on ot.tag_id = t.tag_id ot.id = {$oid};") or die(mysql_error()); while($tag = mysql_fetch_assoc($currenttagsquery)) { $currenttags[] = $tag['tag_name']; } echo json_encode($currenttags);
i've been stuck on of day today , entirely stumped; @ appreciated! thanks!
i recreated example locally, , performs expected using:
$.getjson("getcurrenttags.php", {"id": id}, function(data) { $('#my-tag-list').tags({ tagdata: data, bootstrapversion: "2" }); });
another thing note server side code vulnerable sql injection you're inserting unsanitized value directly sql statement. if available you, recommend use pdo. in case of example, server side code looked this:
$db = new pdo("mysql:host=localhost;dbname=test", 'root', ''); $stmt = $db->prepare( "select tag_name t_tag t join t_object_tag ot on ot.tag_id = t.tag_id ot.id = :id" ); $stmt->bindparam(":id", $_get['id']); $stmt->execute(); $result = $stmt->fetchall(pdo::fetch_assoc); $currenttags = array(); foreach($result $row) { $currenttags[] = $row['tag_name']; } echo json_encode($currenttags);
a introduction pdo can found here.
Comments
Post a Comment