如何使用cakephp 2.x设置输入字段的值


How to set value to input field with cakephp 2.x?

我需要在cakephp 2.x中设置一个输入文本字段的值。

描述:我有一个select字段,用于显示文章列表,根据用户选择的文章,我必须在输入文本字段(只读)中设置这篇文章的价格。

我正在使用javascript。下面是我的代码:

<script type="text/javascript">
    function alerta(a){
           var price = ?//something in a function within my controller.
           document.getElementById("AperturaArticuloPrecio").value = "price";
    }
</script> 

我的输入字段:

echo "<table><tr><td>";
echo $this->Form->input('articulo_id',array('onChange' => 'alerta(1)'));
echo "</td><td>";
echo $this->Form->input('cantidad',array('type' => 'text'));
echo "</td><td>";
echo $this->Form->input('precio',array('type' => 'text','readonly' => 'readonly'));
echo "</td><td>";
echo $this->Form->input('total',array('type' => 'text','readonly' => 'readonly'));
echo "</td></tr></table>";

我明白我的javascript代码中的'a'变量必须是文章的名称,我需要使用mi控制器获得这篇文章的价格,但我不知道如何使用javascript调用控制器。

如果你需要更多的细节告诉我。谢谢你。


更新:

我改变了我的javascript代码为下一个代码:

    <?php
        $this->Js->get('#AperturaArticuloArticuloId')->event('change', '
                //alert($( "#AperturaArticuloArticuloId" ).val());
                $( "#AperturaArticuloPrecio" ).val($( "#AperturaArticuloArticuloId option:selected" ).text());
                //Here something to call my function in my controller
            ');
        echo "<table><tr><td>";
        echo $this->Form->input('articulo_id');
        echo "</td><td>";
        echo $this->Form->input('cantidad',array('type' => 'text'));
        echo "</td><td>";
        echo $this->Form->input('precio',array('type' => 'text','readonly' => 'readonly'));
        echo "</td><td>";
        echo $this->Form->input('total',array('type' => 'text','readonly' => 'readonly'));
        echo "</td></tr></table>";
    ?>

所以我可以得到文章被选中的索引和它的内容,我只需要把这个值发送给我的控制器中的一个函数。我怎么能这么做?-我需要使用AJAX, Json或其他东西吗?

在我的控制器中,我有这个函数,但我不知道如何访问它:

public function getPrice($id = null) {
    $options = array(
        'fields' => array('Articulo.precio'),
        'conditions' => array('Articulo.id' => $id));
    $price = $this->Articulo->find('list', $options);
    echo $price;
}   

如果你能给我一个链接我会很感激的!XD

我在这里搜索:http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html和我混合了更新表单字段与js,但没有显示在提交的数据

我的解决方案是:

    <?php
        $data = $this->Js->get('#AperturaArticuloArticuloId')->serializeForm(array('inline' => true));
        $this->Js->get('#AperturaArticuloArticuloId')->event(
            'change',
            $this->Js->request(
                array(
                    'action' => 'getPrice', 
                    'controller' => 'articulos'
                ),
                array(
                    //'update' => '#AperturaArticuloPrecio',
                    'success' => '
                        $("#AperturaArticuloPrecio").val(data);
                        //More javascript code here
                    ',
                    'data' => $data,
                    'async' => true,    
                    'dataExpression'=>true,
                    'method' => 'POST'
                )
            )
        );
        echo $this->Js->writeBuffer();
        echo "<table><tr><td>";
        echo $this->Form->input('articulo_id');
        echo "</td><td>";
        echo $this->Form->input('cantidad',array('type' => 'text'));
        echo "</td><td>";
        echo $this->Form->input('precio',array('type' => 'text','readonly' => 'readonly'));
        echo "</td><td>";
        echo $this->Form->input('total',array('type' => 'text','readonly' => 'readonly'));
        echo "</td></tr></table>";
    ?>

和在我的控制器中:

public function getPrice() {
    $this->autoRender = false;
    $options = array(
        'fields' => array('Articulo.precio'),
        'conditions' => array('Articulo.id' => $this->request->data['AperturaArticulo']['articulo_id']));
    $price = $this->Articulo->find('list', $options);
    $article_selected = implode(",", $price);
    echo "$".$article_selected;
}   

谢谢大家的帮助!