LoginSignup
1
0

More than 5 years have passed since last update.

EC-CUBEでAjax

Last updated at Posted at 2016-07-29

EC-CUBE (v2.13.5)
PHP (v5.6.10)

サンプルとして
セレクトボックス変更で複数のセレクトボックスの選択肢をかえる場合

Smartyはよく分からんので
分かればもっと綺麗になるやもしれんね

sample.tpl
<script type="text/javascript">
$(function($) {
    $('select[name=A]').change(function() {
        $.ajax({
            type: "POST",
            url:  "/ajax/index.php",
            data: {value:$(this).val()},
            dataType: "json",
            success: function(r) {
                //console.log(r);
                $.each(r, function(k, v) {
                    var $select = $("select[name="+k+"]");
                    $select.text("");
                    $.each(v, function(k2, v2) {
                        $select.append("<option value=\""+v2['id']+"\">"+v2['name']+"</option>");
                    })
                })
            },
            error: function(){
            }
        });
    });
});
</script>

<select name="A">
<option value="1">A1<option>
<option value="2">A2<option>
<option value="3">A3<option>
</select>


<select name="B">
<option value="">-<option>
</select>

<select name="C">
<option value="">-<option>
</select>
/html/ajax/index.php
<?php
require_once '../require.php';
require_once CLASS_REALDIR . 'pages/ajax/LC_Page_Ajax.php';
$objPage = new LC_Page_Ajax();
$objPage->init();
$objPage->process();
/data/class/pages/ajax/LC_Page_Ajax.php
<?php
class LC_Page_Ajax {

    public function init() {}

    public function process() {
        $this->action();
    }

    public function action() {
        $data = self::sample();
        @header('Content-Type: application/json; charset=utf-8');
        echo json_encode($data);
        return;
    }

    public function sample() {
        $value = $_POST['value'];
        $relations = ['B', 'C'];

        $query = new SC_Query();
        foreach ($relations as $r) {
            $cols = "id, name";
            $from = "**table**";
            $where = "**where**";
            $data[$r] = $query->select($cols, $from, $where);
        }
        return $data;
    }

}

セキュリティ云々は各々追記してください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0