2012-02-26 49 views
1

希望有人可以幫我用我的代碼。我正在使用重寫的URL並在窗體上放置了這段代碼。當頁面第一次加載mypage.htm時,默認情況下,type_24複選框被選中。如果他們檢查type_12框,我想type_24框取消選中。PHP複選框和URL重寫

我的問題是,如果我檢查type_12框,頁面刷新和type_12和type_24框被檢查..這不是我想要的。我認爲這是因爲我在我的操作中重新加載了我的重寫URL,因爲它工作正常,如果我只是將php文件作爲操作。

任何想法如何我可以修復我的代碼,以便它只有檢查type_24時檢查type_12?

<form name="frmrefresh" id="frmrefresh" method="post" action="mypage.htm"> 

<input type="checkbox" name="type_12" id="type_12" <?php if(isset($_POST['type_12']) && $_POST['type_12']=="12"){?> checked="checked"<?php }?> value="12" onClick="uncheck24(this);" /> <label>12</label> 

<input type="checkbox" name="type_24" id="type_24" <?php if(isset($_GET['id']) && $_GET['id']!=''){?>checked="checked"<?php }?><?php if(isset($_POST['month_24']) && $_POST['type_24']=="24"){?> checked="checked"<?php }?> value="24" onClick="uncheck12(this);"/> <label>24 Months</label> 

<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" /> 
</form> 

在標題中,我有以下功能:

function uncheck12(obj) 
    { 
    if (obj.checked == true) 
     { 
      document.getElementById("type_12").checked = false; 
      document.frmrefresh.submit(); 
     } 
} 

function uncheck24(obj) 
{ 
     if (obj.checked == true) 
     { 
      document.getElementById("type_24").checked = false; 
      document.frmrefresh.submit(); 
     } 
    } 

回答

0

一些言論:

  • 考慮使用單選按鈕而不是複選框
  • 我想你的意思isset($_POST['type_24'])代替isset($_POST['month_24'])

至於你的問題,我敢打賭,你的重寫規則是這樣的:

RewriteRule ^mypage.htm$ mypage.php?id=5 

這意味着,當形式得到通過POST提交,PHP將仍然設置$_GET['id']變量爲你,因爲它是在查詢字符串。而且,由於您在設置$_GET['id']時檢查了'24'選項,第二個複選框將始終被檢查。 爲了解決這個問題,你可以考慮將檢查$_SERVER['REQUEST_METHOD'] == 'GET'

<form name="frmrefresh" id="frmrefresh" method="post" action="mypage.htm"> 
<input type="checkbox" name="type_12" id="type_12" <?php if(isset($_POST['type_12']) && $_POST['type_12']=="12"){?> checked="checked"<?php }?> value="12" onClick="uncheck24(this);" /> <label>12</label> 
<input type="checkbox" name="type_24" id="type_24" <?php if(isset($_GET['id']) && $_GET['id']!='' && $_SERVER['REQUEST_METHOD']=='GET'){?>checked="checked"<?php }?><?php if(isset($_POST['type_24']) && $_POST['type_24']=="24"){?> checked="checked"<?php }?> value="24" onClick="uncheck12(this);"/> <label>24 Months</label> 
<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" /> 
</form> 
+0

約翰你好,非常感謝你的幫助。但一個問題,當頁面刷新,和我做這種檢查\t \t \t \t 如果(isset($ _ REQUEST [ 'type_24'])&& $ _REQUEST [ 'type_24'] == '24'),它仍然認爲type_24仍然檢查,即使在屏幕上它看起來沒有選中?任何想法爲什麼?謝謝! – gatin 2012-02-26 14:02:01

+0

你能否澄清你的意思是'它仍然認爲type_24仍然被檢查'?你在哪裏以及如何看到這個? – John 2012-02-26 19:40:06