簡體   English   中英

CS-Cart Ajax對div的回應

[英]CS-Cart Ajax response to div

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(設計/主題/響應/模板/視圖/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> 

請嘗試以下解決方案:

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> 

請在send_sms.php替換

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

echo '<div id="result">'.json_encode($response).'</div>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM