We often need to allow users to make multiple choices for processing, such as allowing users to select multiple items on a list and then delete the selected items, etc. Today I will give an example to illustrate how PHP and JavaScript handle multiple selections respectively. What we are doing today is a voting system to vote for items in the itemtable table of the MySQL database, and each individual IP can and can only cast two votes.
The table itemtable is created through the following MySQL statement:
CREATE TABLE `itemtable` (
`id` TINYINT( 4 ) NOT NULL AUTO_INCREMENT,
`name` VARCHAR( 50 ) NOT NULL ,
`votes` SMALLINT (6) NOT NULL,
PRIMARY KEY (`id`)
);
The field "name" is the name of the list item, and "votes" is the number of votes received. We also need to create a table "voteiptable" to record the IP of voting users:
CREATE TABLE `voteiptable` (
`id` SMALLINT( 6 ) NOT NULL ,
`voteip` VARCHAR( 15 ) NOT NULL,
PRIMARY KEY ( `id` )
);
Next we write the file "multivote.php". Today we are going to use a database class file "dbclass.php".
<HTML> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>请您投票</title> <style type="text/css"> <!-- body , td{ font-family: "宋体"; font-size: 12px; } --> </style> </head> <body> <? //包含数据库类文件: include_once("dbclass.php")//检查该IP是否已经投过票了: if($db->getfirst("select * from iptable where voteip='$_SERVER[REMOTE_ADDR]'")){ echo "您已经投过票了,谢谢您的参与!"; } //这是投票项目列表页面: if(!$action){ echo "<table width=200 border=0 cellpadding=2 cellspacing=1>"; echo "<form action='' method='post' name='voteform' onsubmit='return checkform(this)'>"; //我们给每个复选框起这样的名字:check1、check2、check3、……,它们的值分别是项目的id, //这样到时候我们处理起来就比较方便了: $myitems=$db->query("select * from itemtable"); $itemNo=0; while($myitem=$db->getarray($myitems)){ $itemNo++; echo "<tr><td nowrap style='cursor:default' onclick='voteform.check$itemNo.click()'><input name='check$itemNo' type='checkbox' value='$myitem[id]'>$myitem[name]</td></tr> \n"; } echo '<tr><td><input name="action" type="hidden" value="vote"><BR><input name="submit" type="submit" value="投票"> <input name="reset" type="reset" value="重置"></td></tr> '; echo "\n</form>\n </table>"; ?> <!-- 现在编写JavaScript程序初步验证用户表单,只允许用户选择两个项目 --> <script langvage="JavaScript"> <!-- function checkform(myform){ checkedcount=0; //循环检测复选框是否选中,若是,checkedcount加1: for(i=1;i<=<?php echo $itemNo ?>;i++){ mycheck=eval("myform.check"+i); //mycheck是复选框对象 if(mycheck.checked==true){ checkedcount++; } } if(checkedcount==2){ //如果只有两个复选框选中,就提交; return true; }else{ //否则发出警告: alert("对不起,您只能投两个项目!"); return false; } } --> </script> <? } //这是处理用户投票的页面 else if($action="vote"){ //在客户端用javascript检验数据是不安全的,但是可以减轻服务器负担和不用浪费用户时间。 //在服务器再次进行验证经常是必要的。 $itemcount=$db->getfirst("select count(*) as count from itemtable"); //取得项目总数 $checkarray=array(); for($i=1;$i<=$itemcount["count"];$i++){ //和javascript处理的过程是不是很像呢 $mycheck="check$i"; $mycheck=$$mycheck; if($mycheck && $db->getfirst("select * from itemtable where id='$mycheck'")){ //之所以加上$db->getfirst("select * from itemtable where id='$mycheck'")是为防止所投项目id不存在 $checkarray[]=$mycheck; //如果这一项被选中,$checkarray增加一项,值为所选id } } if(count($checkarray)==2){ //如果$checkarray的项数是2,则相应的票数加1: for($i=0;$i<count($checkarray);$i++){ $db->query("update itemtable set votes=votes+1 where id='$checkarray[$i]'"); } //把投票用户的IP记入数据库: $db->query("insert into iptable (voteip) values ('$_SERVER[REMOTE_ADDR]')"); echo "投票成功,谢谢您的参与!"; }else{ //否则: echo "对不起,您只能投两个项目!"; } } ?> </body> </HTML>
We can find that the JavaScript on the client side and the PHP on the server side are multiple There are many similarities in handling item selection, but there are also differences. This is a relatively classic multi-option processing program. It would be simpler if the user's options were not limited.