[英]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.