gpt4 book ai didi

php - phalcon 依赖下拉列表人口与 jquery

转载 作者:行者123 更新时间:2023-11-29 06:37:55 25 4
gpt4 key购买 nike

在 phalcon 框架中,我想填充依赖的下拉类别列表。但我的代码中遇到了一些问题:

#1。在选择类别列表 -> 子类别列表上显示:未定义(它没有用值填充选项)

#2。如果数据库没有数据 console.log 显示: undefined variable :我的 Controller 中的 resData
#3。如果选择值为“0”的类别,则不会再次禁用子类别列表我的代码做错了什么? enter image description here

[模块.php]

use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt;

$di->setShared('view', function () use ($config){
$view = new View();
$view->setViewsDir(APP_PATH . $config->appB->viewsDir);
# Register Volt Template
$view->registerEngines(array(
".volt" => function($view, $di) use ($config) {
$volt = new Volt($view, $di);
$volt->setOptions(
array(
'compiledPath' => APP_PATH . $config->appB->cacheDir,
'compiledExtension' => '.php',
'compiledSeparator' => '_',
'compileAlways' => true,
'autoescape' => false,
'stat' => true
)
);
$compiler = $volt->getCompiler();
$compiler->addFunction('strtotime','strtotime');

return $volt;
}
));
return $view;
});

[ Controller ]

public function entryAction()
{
$formAction = 'backend/index/insert';
$this->view->setVar('action',$formAction);
$this->view->product_title = '';
$this->view->product_price = '';
$this->view->product_keyword = '';
$this->view->product_image = '';
$this->view->product_desc = '';
$category = Categories::find();
$this->view->setVar('categories',$category);
$this->view->pick("index/entry");
}

public function getSubcategoryAction()
{
$id = $this->request->getPost('id');
$data = Subcat::findBycategory_id($id);
$resData = array();
foreach($data as $result)
{
$resData[] = array('id' => $result->id, 'category_id' => $result->category_id, 'subcategory' => $result->subcategory_name);
}
echo(json_encode($resData));
//$this->view->setVar('subcategory',$resData);
}

[进入电压]

Category:<select name="category" id="category">
<option value="0">Choose Category ...</option>
{% for category in categories %}
<option value="{{category.id}}">{{category.categoryname}}</option>
{% endfor %}
</select><br/>
sub-Category:<select name="subcategory" id="subcategory" disabled="disabled"><option value="0">Choose Sub-Category ...</option></select>
<br/>
Products:<select name="products" id="products" disabled="disabled"><option value="0">Choose a Product ...</option></select>
<br/>

[JQUERY]

$("select[name='category']").on("change", function(e){
e.preventDefault();
var value = $(this).val();
$("select[name='subcategory']").attr("disabled", false);
$.ajax({
type: "POST",
url: "http://localhost/shopping/backend/index/getSubcategory",
data:'id='+value,
}).done(function(response){
$("#subcategory").not(":first").remove();
response = JSON.parse(response);
response.forEach(function(value){
$('#subcategory').append('<option value="'+value.id+'">'+value.subcategory+'</option>');
});

}).fail(function(){
console.log('error: Please reload page and try again!');
}).always(function(){
console.log('Complete:');
});
});

最佳答案

请注意,在 Controller 的第一行中,您禁用了 View ,因此永远不会处理 Volt。正如您现在所做的那样,jQuery 仅接收 JSON 结果,因此您要附加 JSON 而不是 Volt。您必须选择一条路径:要么使用 Volt,在这种情况下,您必须删除两个操作中的第 1 行并使用参数处理 View ,要么继续将 JSON 数据发送到 jQuery,并正确设置它以处理 JSON 响应(检查这个answer)

在您的情况下,getSubcategoryAction() 看起来像:

public function getSubcategoryAction()
{
//$this->view->disable(); //Replaced by:
$this->view->setRenderLevel(
View::LEVEL_ACTION_VIEW
);
$id = $this->request->getPost('id');
$data = Subcat::findBycategory_id($id);
foreach($data as $result)
{
$resData[] = array('id' => $result->id, 'category_id' => $result->category_id, 'subcategory' => $result->subcategory_name);
}

$this->view->setVar('categories', $resData);

}

假设您已在 DI 中将 Volt 设置为渲染引擎,并且您的 Volt 模板对应于 ../app/views/index/getSubcategory.phtml,即:

<?php

use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt;

// Register Volt as a service
$di->set(
'voltService',
function ($view, $di) {
$volt = new Volt($view, $di);

$volt->setOptions(
[
'compiledPath' => '../app/compiled-templates/',
'compiledExtension' => '.compiled',
]
);

return $volt;
}
);

// Register Volt as template engine
$di->set(
'view',
function () {
$view = new View();

$view->setViewsDir('../app/views/');

$view->registerEngines(
[
'.volt' => 'voltService',
]
);

return $view;
}
);

Volt 注册码复制自:https://docs.phalconphp.com/en/3.4/volt根据您的应用程序结构修改目录。

关于php - phalcon 依赖下拉列表人口与 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52963830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com