1

I want to put a form on my website for giving away 3 prizes. Each member to be able to select only 2 item on the list that they wish to win.

I have created the form and with JavaScript it works fine, but it's client side and can't be trusted.

What is the best way to implement such thing and be sure that each user can ONLY select 2 item? best way to do it with PHP?

Appreciate any help.

4

3 回答 3

0

为什么不使用单选按钮?

<form method="post">
     Price 1 <input type="radio" value="price 1" />
     Price 2 <input type="radio" value="price 2" />
     Price 3 <input type="radio" value="price 3" />
</form>

如果您想允许多个奖品,只需切换到复选框

<form method="post">
     Price 1 <input type="checkbox" name="price[]" value="price 1" />
     Price 2 <input type="checkbox" name="price[]" value="price 2" />
     Price 3 <input type="checkbox" name="price[]" value="price 3" />
</form>
于 2012-12-24T21:57:10.953 回答
0

jQuery 是老大,看看这里。

http://gravitywiz.com/2012/06/11/limiting-how-many-checkboxes-can-be-checked/

于 2012-12-24T22:00:28.647 回答
0
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Price 1<input value="price1" type="checkbox" name="price[]">
Price 2<input value="price2" type="checkbox" value="price[]">
Price 3<input value="price3" type="checkbox" value="price[]">
<input type="submit" name="send" value="Send" />
</form>

然后检查数组的大小$_POST['price']

<? 
   if(isset($_POST['send'])) {
       if(sizeof($_POST['price']) == 2) {
           //here you go
       } else { 
          echo "error, pick exactly two prices!";
       } 
   }

?>
于 2012-12-24T22:03:27.630 回答