【问题标题】:CS-Cart Ajax response to divCS-Cart Ajax 对 div 的响应
【发布时间】:2015-08-10 08:53:54
【问题描述】:

CS-Cart ajax 工作正常,我也收到了响应,但是如何在我的 view(checkout.tpl) 文件中将此响应与 html/js 一起使用。

控制器(前端):send_sms.php

use Tygh\Registry;
use Services_Twilio;
use Tygh\Ajax;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 if ($mode == 'send_sms') {

    $status = 1;
    $response = array(
        'status' =>$status,
        'data' =>'Hello World',
    );

   if($status == 1) {
        fn_set_notification('N', fn_get_lang_var('success'), fn_get_lang_var('sms_sent'), true);
    } else {
        fn_set_notification('E', fn_get_lang_var('warning'), fn_get_lang_var('sms_failed'), true);

    }

    $val=json_encode($response);

    Registry::get('ajax')->assign('result', $val);

 }
 exit;
}

查看 checkout.tpl (design/themes/responsive/templates/views/checkout/checkout.tpl)

<div id="result">
    <!-- id="result" -->
    <!-- result -->
</div>
<h2>Verify your number</h2>

<form class="cm-ajax" action="index.php" method="post" name="send_sms">


    <input type="hidden" name="result_ids" value="result" />

    <div class="form-control send_sms_block">
        <input type="text" name="country_code" id="country_code" disabled value="+92"/>
        <input type="text" name="carrier_code" id="carrier_code" disabled value="300"/>
        <i class="ty-icon-down-micro open_cr"></i>
        <input type="text" name="phone" id="phone"/>

        <div class="carrier_list hidden">
            <ul>
                <li>301</li>
                <li>302</li>
            </ul>
        </div>
    </div>
    <div class="clearfix"></div>
    <input class="ty-btn ty-btn__big" id="send_sms" type="submit" value="Send Pin" name="dispatch[send_sms.send_sms]"/>
</form>

【问题讨论】:

    标签: javascript php jquery ajax cs-cart


    【解决方案1】:

    请尝试以下解决方案:

    php 文件:

    use Tygh\Registry;
    use Services_Twilio;
    use Tygh\Ajax;
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
     if ($mode == 'send_sms') {
    
        $status = 1;
        $response = array(
            'status' =>$status,
            'data' =>'Hello World',
        );
    
       if($status == 1) {
            fn_set_notification('N', fn_get_lang_var('success'), fn_get_lang_var('sms_sent'), true);
        } else {
            fn_set_notification('E', fn_get_lang_var('warning'), fn_get_lang_var('sms_failed'), true);
    
        }
    
        $val=json_encode($response);
    
        Registry::get('view')->assign('result', $val);
        Registry::get('view')->display('views/path/to/tpl/file.tpl');
     }
     exit;
    }
    

    tpl 文件:

    <div id="result">
    {if $result}{$result}{/if}
    <!--result--></div>
    
    <h2>Verify your number</h2>
    
    <form class="cm-ajax" action="index.php" method="post" name="send_sms">
    
    
        <input type="hidden" name="result_ids" value="result" />
    
        <div class="form-control send_sms_block">
            <input type="text" name="country_code" id="country_code" disabled value="+92"/>
            <input type="text" name="carrier_code" id="carrier_code" disabled value="300"/>
            <i class="ty-icon-down-micro open_cr"></i>
            <input type="text" name="phone" id="phone"/>
    
            <div class="carrier_list hidden">
                <ul>
                    <li>301</li>
                    <li>302</li>
                </ul>
            </div>
        </div>
        <div class="clearfix"></div>
        <input class="ty-btn ty-btn__big" id="send_sms" type="submit" value="Send Pin" name="dispatch[send_sms.send_sms]"/>
    </form>

    【讨论】:

      【解决方案2】:

      请替换 send_sms.php

      $val=json_encode($response);
      Registry::get('ajax')->assign('result', $val);
      

      echo '&lt;div id="result"&gt;'.json_encode($response).'&lt;/div&gt;';

      【讨论】:

      • 在 cs-cart 上下文中更好的选择是 Registry::get('ajax')-&gt;assignHtml('result' . (empty($_REQUEST['additional_id']) ? '' : '_' . $_REQUEST['additional_id']), json_encode($response)); 它只会在我的#result div 中打印内容,但假设一旦 ajax 请求完成,我如何通过 js 发出警报或其他事情。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 2019-03-29
      • 2018-02-23
      • 2014-08-31
      • 2015-12-28
      • 1970-01-01
      相关资源
      最近更新 更多