我有一个带有两个提交按钮的注册表。一个提交按钮用于免费会员帐户,另一个用于高级会员帐户。
我的表单代码是
<form action="post.php" method="post">
<input type="text" name="name" />
<input type="text" name="mail" />
//submit buttons
<input type="submit" value="signup for free member" />
<input type="submit" value="signup for premium member" />
</form>
if($_POST['name'] and $_POST['mail']){
$user_name = $_POST['name'];
$mail = $_POST['mail']
//How i can know he is preimum or free ?
}
现在如何判断用户是否点击了免费按钮或高级按钮?
示例:
if($_POST['free_member']){
$member = 'free';
}else{
$member = 'premium';
}
答案 0 :(得分:8)
HTML
<input type="submit" value="signup for free member" name="signup_free"/>
<input type="submit" value="signup for premium member" name="signup_premium"/>
PHP
if($_POST['signup_free'])
{
$member = 'free';
}
elseif($_POST['signup_premium'])
{
$member = 'premium';
}
答案 1 :(得分:7)
您可以轻松地为提交按钮指定名称:
<input name="submit" type="submit" value="signup for free member" />
<input name="submit" type="submit" value="signup for premium member" />
然后您的帖子会显示如下:
var_dump($_POST['submit']); // 'signup for premium member'
这不是很容易扩展。我建议使用javascript填充提交时的隐藏字段。示例:
HTML:
<!-- Include JQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<input type="hidden" id="program_type" name="program_type" value="" />
<input class="submit" type="submit" value="signup for free member" data-program-type="free" />
<input class="submit" type="submit" value="signup for premium member" data-program-type="premium" />
JQuery:
$('.submit').click(function(e){
e.preventDefault(); // Stall form submit
$('#program_type').val($(this).data('program-type'));
$(this).parents('form:first').submit(); // Submit form
});
<强> PHP:强>
var_dump($_POST['program_type']); // 'free' or 'premium'