2014-03-26 25 views
0
public function removeSelectedCategory() 
     { 
      echo "<script>var r=confirm('Are you sure you want to delete this category?');   </script>"; 

      if($r==true) 
      { 
       some code goes here 
      } 

我寫過類似這樣的東西。現在我想刪除一些內容,但我希望用戶在刪除前確認它。該功能位於控制器內部。如何在控制器的函數內使用php codeigniter中的確認框?

+0

你**不應該**在PHP中編寫JavaScript。 –

+0

你知道,PHP是服務器端的,JS是客戶端語言,對吧?你不能像那樣混合他們。 –

+0

如何確認?有沒有其他方法? – John

回答

2

您可以在生成按鈕,鏈接或任何需要確認時簡單地提供JavaScript代碼。例如:

<a href="..." onclick="return confirmDelete();">Delete category</a> 
<!-- or --> 
<input type="submit" name="delete" value="Delete" onclick="return confirmDelete();" /> 

<script type="text/javascript"> 
    function confirmDelete() { 
     return confirm('Are you sure you want to delete this category?'); 
    } 
</script> 

只有當用戶在確認對話框中按下「確定」時,鏈接/按鈕纔會被激活。

相關問題