ขอรบกวนถามววิธีสร้าง

Moderator: mindphp

spawat
PHP Newbie
PHP Newbie
Posts: 7
Joined: 02/11/2019 4:34 pm

ขอรบกวนถามววิธีสร้าง

Post by spawat »

ขอรบกวนถามววิธีสร้าง
Dropdown list จังหวัด อำเภอ ตำบล เพียงใส่รหัสจังหวัด ใน MooZiicart ได้อย่างไรเพื่อให้สมาชิกสามารถเลือกตรง Dropdownได้เลยจากโทรศัพท์

User avatar
jamepiyawat
PHP VIP Members
PHP VIP Members
Posts: 2008
Joined: 21/05/2019 10:45 am

Re: ขอรบกวนถามววิธีสร้าง

Post by jamepiyawat »

ถ้าเป็น MooZiiCart สามารถใส่โค้ดได้ตามนี้เลย ครับ

Code: Select all

<?php
            if ($config_fields['state']['display'])
            {
                ?>
                <div class = "">
                    <div class = "control-label name">
                        <?php print _MZC_STATE ?>
                        <?php
                        if ($config_fields['state']['require'])
                        {
                            ?><span>*</span><?php } ?>
                    </div>
                    <div class = "controls">
                        <select name="state" id="state" class = " state-ajax">
                            <?php foreach ($this->provinces as $province): $selected = ($province->text == $this->user->state) ? 'selected' : ''; ?>
                                <option value="<?php echo $province->value; ?>" <?php echo $selected; ?> ><?php echo $province->text; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
            <?php } ?>
            <?php
            if ($config_fields['city']['display'])
            {
                ?>
                <div class = "">
                    <div class = "control-label name">
                        <?php print _MZC_CITY ?> <?php
                        if ($config_fields['city']['require'])
                        {
                            ?><span>*</span><?php } ?>
                    </div>
                    <div class = "controls">
                        <select name="city" id="city" class = " city-ajax">
                            <?php foreach ($this->amphurs as $amphur): $selected = ($amphur->text == $this->user->city) ? 'selected' : ''; ?>
                                <option value="<?php echo $amphur->value; ?>" <?php echo $selected; ?> ><?php echo $amphur->text; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <div class = "">
                    <div class = "control-label name">
                        <?php print _MZC_AMPHUR ?>
                    </div>
                    <div class = "controls">
                        <select name="district" id="district" class = " district-ajax">
                            <?php foreach ($this->districts as $district): $selected = ($district->text == $this->user->district) ? 'selected' : ''; ?>
                                <option value="<?php echo $district->value; ?>" <?php echo $selected; ?> ><?php echo $district->text; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
            <?php } ?>
            <?php
            if ($config_fields['zip']['display'])
            {
                ?>
                <div class = "">
                    <div class = "control-label name">
                        <?php print _MZC_ZIP ?> <?php
                        if ($config_fields['zip']['require'])
                        {
                            ?><span>*</span><?php } ?>
                    </div>
                    <div class = "controls">
                        <input type = "text" name = "zip" id = "zip" value = "<?php print $this->user->zip ? $this->user->zip : (isset($this->amphurs[0]->POSTCODE) ? $this->amphurs[0]->POSTCODE : '') ; ?>" class = "input" />
                    </div>
                </div>
            <?php } ?>
หน้าตาก็จะประมาณนี้
Selection_999(1444).png
Selection_999(1444).png (9.64 KiB) Viewed 672 times
แล้วให้เราสร้างไฟล์ js ขี้นมาใส่โค้ดตามนี้ครับ

Code: Select all

defined('_JEXEC') or die('Restricted access');

$script_register_fields = '';
foreach ($config_fields as $key => $val)
{
	if ($val['require'])
	{
		$script_register_fields = "register_field_require['" . $key . "']=1;";
	}
}

\Joomla\CMS\Factory::getDocument()->addScriptDeclaration("
	var urlcheckpassword = '" . $this->urlcheckpassword . "';
        var register_field_require = {};
	" . $script_register_fields . "
	jQuery(function ($) {
                $('.state-ajax').change(function () {
                        var t = $(this);
                        $.post('" . JUri::root() . "index.php?option=com_mooziicart&controller=user&task=amphurs', {'province_id': $(this).val()}, function (result) {
                                t.closest('div.jshop_register').find('.city-ajax option').remove();
                                var city = t.closest('div.jshop_register').find('.city-ajax');
                                for (var i = 0; i < result.length; i++) {
                                        $('<option value=\"' + result[i].value + '\" data-postcode=\"' + result[i].POSTCODE + '\">' + result[i].text + '</option>').appendTo(city);
                                }
																$('.city-ajax').trigger('liszt:updated');
                                districts(city);
                                t.closest('.jshop_register').find('#zip, #d_zip').val(result[0].POSTCODE);
                        });
                });
                $('.city-ajax').change(function () {
                        $(this).closest('.jshop_register').find('#zip').val($('#city option:selected').attr('data-postcode'));
                        $(this).closest('.jshop_register').find('#d_zip').val($('#d_city option:selected').attr('data-postcode'));
                        districts($(this));
                });
                function districts(t) {
                        var amphur_id = t.val();
                        $.post('" . JUri::root() . "index.php?option=com_mooziicart&controller=user&task=districts', {'amphur_id': amphur_id}, function (result) {
                                t.closest('div.jshop_register').find('.district-ajax option').remove();
                                var district = t.closest('div.jshop_register').find('.district-ajax');
                                for (var i = 0; i < result.length; i++) {
                                        $('<option value=\"' + result[i].value + '\">' + result[i].text + '</option>').appendTo(district);
                                }
																$('.district-ajax').trigger('liszt:updated');
                        });
                }
        });
");

อย่าลืมเรียกไฟล์ js มาใช้แบบนี้ด้วยนะ ครับ

Code: Select all

include(dirname(__FILE__) . "/register.js.php");
ทำไม่ได้ยังไงแจ้งมาได้เลยนะ ครับ

Post Reply

Return to “ปัญหาการใช้ MooZiiCart”

Who is online

Users browsing this forum: No registered users and 2 guests