不能触发一次两种形式,你一次只能发送一个请求到PHP页面,除非你正在使用AJAX。这里
的解决方案将是隐藏第二形式,除非该复选框被附接,并且其显示为第一种形式的一部分,当该复选框被选中。
你想我证明你如何能做到这一点?
更新
显示/隐藏的通讯形式上的复选框的状态的变化:
HTML:
<form>
<input type="text" name="firstname">
<input type="text" name="email">
<input type="checkbox" name="newsletter" value="1">
<div id="newsletter" style="display:none;">
<input type="text" name="news-firstname">
<input type="text" name="news-email">
</div>
<input type="submit" name="submit">
</form>
的Javascript(jQuery的):
// Set the onchange event for the checkbox
$('input[name="newsletter"]').change(function(){
// Test is checkbox is checked
if($(this).is(':checked')){
// If it is checked then show the newsletter
$('#newsletter').show();
}else{
// If it is not checked then hide the news letter
$('#newsletter').hide();
}
})
我没有测试过上述内容,但它应该可以工作,如果有任何错误,让我知道,我会解决它们。我已经明确提出意见来解释每一步正在做什么。
在你的PHP脚本,那么你必须测试,看是否复选框被选中,然后相应地验证。否则,您将始终收到通讯信息。仅仅因为他们被隐藏并不意味着他们没有被提交。
我建议将两个单独的PHP脚本合并到一个文件中,这会使它更简单。
的替代品,这是AJAX,这是一个稍微复杂一些,但它是很好的做法,因为越来越多的人现在使用它。如果你想看到这个例子,请让我知道。
“触发”你的意思是显示/显示或提交? – Stefan 2012-01-31 15:40:44
你想提交这两个表单,还是第二个而不是第一个? – 2012-01-31 15:40:59
@Stefan提交.... – user1091558 2012-01-31 16:02:21