CS-Cart Ajax对div的响应

时间:2015-08-10 08:53:54

标签: javascript php jquery ajax cs-cart

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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

请替换 send_sms.php

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

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