php - How to display json data in a smarty template? -
i'm using smarty , php website. there code converts data json. want use data in smarty template. data set of error messages , want display messages in assigned smarty template @ desired id. i'm not able in smarty template. happening error messages displayed on plain page instead in desired tag. following smarty , php code: following smarty code want display error messages:
if $error_msg}<div class="error-info">{$error_msg.error_msgs}</div>{/if} now following php code:
<?php if($request['form_submitted']=='yes') { $ret = $objpracticesheet->insertpracticesheet($request, $practice_sheet_error_messages); if(!$ret) { $error_msg = $objpracticesheet->getallerrors(); $data = array(); $data['error_message'] = $error_msg['error_msgs']; $data = json_encode($data); echo $data; die; } else { $data = array(); $data['success_message'] = "success"; $data = json_encode($data); echo $data; die; } } else { $all_subjects = $objsubjectstopicsques->getallsubjectshavingtopics(); $smarty->assign('all_subjects', $all_subjects); $smarty->assign('sheet_type', 'practice'); $bread_crumbs_text = 'add practice sheet'; $submit_value = 'submit'; $cancel_value = 'cancel'; $file_to_show = 'manage-practice-sheet.tpl'; } $smarty->assign("op", $op); $smarty->assign("query_string", $query_string); $smarty->assign("bread_crumbs_text", $bread_crumbs_text); $smarty->assign("submit_value", $submit_value); $smarty->assign("cancel_value", $cancel_value); $smarty->assign("error_msg", $error_msg); $smarty->assign("file_to_show", $file_to_show); /*$smarty->assign('create', '-active'); $smarty->assign("sub_menu_file", "epn-create-sub-menu.tpl"); $smarty->assign('practice_sheet', '-active');*/ $smarty->assign('practice_sheet', 'active'); $smarty->assign('prepare', 'selected'); $smarty->display("index.tpl"); ?> can me in displaying error messages json data in above div? in advance. i'm attaching screenshot of current output.
use javascript parse json, put errors in div.
to make easier, let's give div id:
<div id="error-info-main" class="error-info"></div> then
<script> {literal} (function() { var error_json = {/literal}{$error_msg.error_msgs}{literal}; var errors = json.parse(error_json); document.getelementbyid('error-info-main').innerhtml = errors.error_message; }()); {/literal} </script> alternatively, pass message php:
$smarty->assign("error_msg", $error_msg->error_message); this may not 100% correct, don't have data test with, believe that's right. idea @ rate.
Comments
Post a Comment