2014-02-12 36 views
1

我想創建一個按鈕,提交表單然後重定向到另一個頁面。CakePHP提交表單,然後重定向不工作

$('#saveApp').click(function() { 
     event.preventDefault(); 
     $("form[id='CustomerSaveForm']").submit(); // use the native submit method of the form element 
     window.location.href = "<?php echo EWConfig::$URL?>/ExpressWay/ProgramApplications/sales/residence"; 

     return true; 
    }); 

這將重定向而不是提交表單雖然註釋掉event.preventDefault()卻反其道而行之。

+1

任何理由不從服務器端重定向? –

+0

它只在這個按鈕上點擊重定向。另一個按鈕提交表單並且不重定向。所以我不認爲服務器端是最好的選擇。 – Subie

+0

你可以使用'ajax'然後''.post(...)'使用'done'事件來確定何時重定向(有助於排序驗證等),更多信息在這裏https://api.jquery。 com/jQuery.post/ –

回答

0

使用服務器端解決方案結束。

查看

echo $this->Form->button('Save and Redirect', array('name' => 'data[redirect]')); 

控制器

if(!is_null($this->request->data('redirect'))){ 
    $this->redirect(array('controller' => 'Program', 'action' => 'index')); 
}