多个提交按钮

发布于 2024-11-30 09:50:45 字数 682 浏览 0 评论 0原文

我有一个带有两个提交按钮的注册表单。一个提交按钮适用于免费会员帐户,另一个提交按钮适用于高级会员帐户。

我的表单代码是

<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';
}

I have a registration form with two submit buttons. One submit button is for a free member account, and the other is for a premium member account.

My form code is

<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 ?
    }

Now how can I tell whether the user clicked on the free button or the premium button?

example:

if($_POST['free_member']){
  $member = 'free';
}else{
  $member = 'premium';
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海夕 2024-12-07 09:50:45

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';
}

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';
}
故事未完 2024-12-07 09:50:45

您可以轻松地为提交按钮命名:

<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'

You could easily give the submit buttons a name:

<input name="submit" type="submit" value="signup for free member" />
<input name="submit" type="submit" value="signup for premium member" />

Then your post will show like this:

var_dump($_POST['submit']); // 'signup for premium member'

This isn't very scalable. I'd recommend using javascript to populate a hidden field on submit. Example:

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文