2015-10-19 89 views
0

我需要爲cakephp 2.x中的輸入文本字段設置一個值。如何用cakephp 2.x設置輸入字段的值?

說明: 我有一個選擇字段,其中顯示文章列表,具體取決於用戶選擇的文章,我必須在輸入文本字段(只讀)中設置此文章的價格。

我正在使用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

+0

你需要設置一個值,爲什麼你在這裏使用javascript? ('total',array('type'=>'text','readonly'=>'readonly'value ='myvalue')); –

+0

使用onchange函數並通過AJAX調用您的結果。 –

回答

0

嗯,我正在尋找在這裏:http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html和我一起update form field with js but not showing in submitted data

我的溶液混合是:

<?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; 
} 

謝謝大家的幫助我!

相關問題