dqoqnmb163241 2013-08-08 16:14
浏览 55

如何在智能模板中显示json数据?

I'm using smarty and php for my website. There is some code which converts the data into json. I want to use that data in a smarty template. That data is a set of error messages and I want to display those messages in a assigned smarty template at desired ID. I'm not able to get that in smarty template. Actually what is happening is the error messages are displayed on a plain page instead in the desired tag. Following is my smarty and PHP code: Following is the smarty code where I want to display the error messages:

if $error_msg}<div class="error-info">{$error_msg.error_msgs}</div>{/if}

Now following is my 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 you help me in displaying the error messages json data in the above div? Thanks in advance. I'm also attaching the screenshot of the current output.enter image description here

  • 写回答

1条回答 默认 最新

  • douzi7711 2013-08-08 16:54
    关注

    Use JavaScript to parse the JSON, then put the errors in the div.

    To make this easier, let's give your div an 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, you could just the pass the message itself from PHP:

    $smarty->assign("error_msg", $error_msg->error_message);
    

    This may not be 100% correct, as I don't have your data to test with, but I believe that's right. You get the idea at any rate.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?