如何用下拉时选定的值来设置$cli?
chartsController.php
public function actionIndex()
{
...
// Trae todos los datos del cliente seleccionado
$cli = 1; //POST value de dropdown <<----<----
$cli_sleccionado = (new \yii\db\Query())
->select('client_id, prod_id, order, survey_date')
->from('survey')
->where('client_id=:id',array(':id'=>$cli))
->orderBy( ['client_id'=>SORT_ASC, 'order' => SORT_DESC,])
->all();
return $this->render('index', [
'cli_sleccionado' => $cli_sleccionado,
]);
}index.php (视图文件)
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Id
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<?php
foreach($tot_cli as $key) {
echo '<li value="'.$key['client_id'].'"><a href="#">'.$key['client_id'].'</a></li>';
}
?>
</ul>
<i><b>...Por favor, selecciona un cliente. </b></i>
</div>

我想可能是通过Ajax,但我迷路了。
发布于 2015-11-13 14:27:54
这里有一些想法..。
<form id="someForm" method="post" action="urlToPostTo">
<input type="hidden" id="someValue" name="someValue">
</form>
<ul id="list">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
</ul>然后使用jquery
$("#list li a").click(function(){ // Here we listen for a click on a link within the <li>
var selectedValue = $(this).html(); // Put the value of the link into a variable
// You could send this value to a PHP script using Ajax (in the background)
$.post("some-script.php?id=" + selectedValue, function(response){
// Place the response from the PHP script into an element
$("#myElement").html(response);
});
// Or you can set the value of a hidden field in a form, and submit the form
// This option will refresh your page
$("#someValue").val(selectedValue);
$("#someForm").submit();
return false;
});使用Ajax的想法是将值提交给PHP脚本,PHP脚本从数据库中获取数据,然后简单地回显一些HTML内容。这个HTML内容将通过'repsonse‘变量在您的jQuery中可用。然后,将页面上元素的HTML内容设置为PHP脚本生成的HTML (即包含数据的表)。
发布于 2015-11-17 13:21:43
首先,我把所有的数据通过控制器
chartsController.php
// Array con todos los pedidos
$surveys = \backend\models\Survey::find()->asArray()->orderBy(['order' => SORT_DESC,])->all();index.php (视图)
<script type="text/javascript">
// **********ENVIO DE SELECCION DEL DROPDOWN**************** devuelve un array con los pedidos del cliente
var surveys=<?php echo json_encode($surveys) ?> ;
//Inicializo la funcion CON EL 1er cliente
var id_seleccionado = <?php echo $tot_cli[0]['client_id'] ?>;
var cliente = buscarCliente(id_seleccionado);
cliente_cero (cliente); //llamo funcion cliente_cero
function buscarCliente(idCliente){
var pedidosCliente=[];
for (s in surveys)
if (surveys[s].client_id==idCliente)
pedidosCliente.push(surveys[s]);
return pedidosCliente;
}
//id_seleccionado se actualiza segun el id elegido
function mostrarResultados(idCliente){
cliente = buscarCliente(idCliente);
cliente_cero (cliente);
drawChart(); //dibujo google chart
}
//cargar el resto del array con 0, en el caso que contenga datos vacios
function cliente_cero (cliente){
while (cliente.length <5){
cliente[cliente.length]= "";
}
}
...
</script>
<body>
...
<!-- DROPDOWN-->
<div class="dropdown" >
<br><i><b>...Por favor, selecciona un cliente. </b></i>
<select class="form-control" id="client" name="client" onChange="mostrarResultados(this.value);">
<?php
foreach($tot_cli as $key) {
echo '<option value="'.$key['client_id'].'">'.$key['client_id'].'</option>';
}
?>
</select>
</div> <!--finDropDown -->..。
https://stackoverflow.com/questions/33694688
复制相似问题