我有一个带两个提交按钮的注册表.一个提交按钮用于免费会员帐户,另一个用于高级会员帐户.
我的表格代码是
<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'; }
您可以轻松地为提交按钮指定名称:
<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'