2012-02-26 67 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