Hello,
I keep getting [term] in my querystring resulting in a 400 bad request.
Any help is appreciated
The string looks like
http://yii2shop.local/site/brandlist?search%5Bterm%5D=me
bud should look like
http://yii2shop.local/site/brandlist?search=me
In my helper file i have
public static function brandbyid($id=1) {
$sql = 'SELECT id, brand '
. 'FROM brand '
. 'WHERE id="'.$id.'" ';
$brand = Brand::findBySql($sql)->asArray()->all();
return $brand;
}
in my _form I use
$url = \yii\helpers\Url::to(['/site/brandlist']);
/* @var $this \yii\web\View */
/* @var $content string */
// Script to initialize the selection based on the value of the select2 element
$initScript = <<< SCRIPT
function (element, callback) {
var id=\$(element).val();
if (id !== "") {
\$.ajax("{$url}?id=" + id, {
dataType: "json"
}).done(function(data) { callback(data.results);});
}
}
SCRIPT;
An the form field
<?= $form->field($model, 'id')->widget(Select2::classname(), [
'options' => ['placeholder' => 'Search for a brand ...'],
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 2,
'ajax' => [
'url' => $url,
'dataType' => 'json',
'data' => new JsExpression('function(term,page) { return {search:term}; }'),
'results' => new JsExpression('function(data,page) { return {results:data.results}; }'),
],
'initSelection' => new JsExpression($initScript)
],
]);?>