2013-08-22 81 views
2

我想解決一個表單提交的問題,我一直無法理解。當我第一次提交表格時,在更改下拉列表的值後,$this->request->data數組爲空。如果我再次提交,我會看到我期望的。每次更改表單上的下拉菜單時都會發生這種情況。Cakephp表單必須提交兩次才能工作

這裏是形式:

<?php 

echo $this->Form->create('Refine', array('url' => '/ServiceDirectoryResults/refine')); 

echo $this->Form->input('state', array(
    'type' => 'select', 
    'label' => 'State', 
    'options' => $all_states, 
    'selected' => array('state_selected', $state_selected), 
    'id' => 'state', 
    )); 

echo $this->Form->input('solution', array(
    'type' => 'select', 
    'label' => 'Solution', 
    'options' => $solutions, 
    'selected' => array('selected', $solution), 
    'id' => 'solutions', 
    )); 

echo $this->Form->input('region', array(
    'before' => '<fieldset id="Region">', 
    'multiple' => 'checkbox', 
    'options' => $regions, 
    'selected' => $reg_selected, 
    'after' => '</fieldset>' 
    )); 

echo $this->Form->input('tags', array(
    'before' => '<fieldset id="TagBox">', 
    'multiple' => 'checkbox', 
    'options' => $narrow, 
    'selected' => $tag_selected, 
    'after' => '</fieldset>' 
    )); 

echo $this->Form->end('Refine Search'); 

?> 

的形式呈現的罰款。如果更改狀態或解決方案下拉列表並提交表單,$this->request->data陣列爲空。如果我再次提交而不更改任何內容,則該數組包含我期望看到的內容。

在我的控制器我有

if(isset($this->request->data['Refine']['state'])) 
{ 
    $state = $this->request->data['Refine']['state']; 
} 

顯然,如果數組爲空,我什麼也得不到的狀態變量在第一時間提交表單。

如果有人能夠對此行爲有所瞭解,我將不勝感激。我的表單創建過程中有過錯嗎?

這裏要求的是與此表單一起使用的js。這個想法是,如果「全部」複選框(這是爲控制器中的兩個區域和標籤創建的第一個複選框)設置或清除複選框。

$(document).ready(function(){ 

    $("#RefineRegion0").click(function(){ 

     if ($("#Region #RefineRegion0").is(':checked')) { 
      $("#Region input[type=checkbox]").each(function (e) { 
       $(this).prop("checked", true); 
      }); 

     } else { 
      $("#Region input[type=checkbox]").each(function (e) { 
       $(this).prop("checked", false); 
      }); 
     } 
    }); 

    $("#RefineTags0").click(function(){ 
     if ($("#TagBox #RefineTags0").is(':checked')) { 
      $("#TagBox input[type=checkbox]").each(function (e) { 
       $(this).prop("checked", true); 
      }); 

     } else { 
      $("#TagBox input[type=checkbox]").each(function (e) { 
       $(this).prop("checked", false); 
      }); 
     } 
    }); 

    $("#RefineViewForm").submit(function(){ 
     if($('#state').val() == "" || $('#solutions').val() == ""){ 
      alert("Please select a State and Solution before continuing") 
     } 
    }); 
}); 

希望幫助

+0

不知道,出了什麼問題。但只是嘗試從表單輸入選項中刪除'selected'屬性,因爲它在** cakephp2.0 **中已棄用。 你可以檢查[這裏](http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#updates) –

+0

請寫下你的js代碼 –

+0

我編輯了我的原始問題並添加了JS在這個表單上使用。 – rickl

回答

0

我注意到兩兩件事:

1)形式的網址:控制器名稱是小寫的,並強調:service_directory_results。看到cakephp名字召集:http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html。但我認爲它能夠更好地使用數組的URL,這樣你的路由 可以匹配:

echo $this->Form->create('Refine', array('url' => array('controller' => 'service_directory_results', 'action' => 'refine'))); 

2)在您的js如果這些字段是空不發送後增加回報虛假的; (也缺少一個;)

$("#RefineViewForm").submit(function(){ 
    if($('#state').val() == "" || $('#solutions').val() == ""){ 
     alert("Please select a State and Solution before continuing"); 
     return false; 
    } 
});