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".
请您投票 //包含数据库类文件: include_once("dbclass.php")//检查该IP是否已经投过票了: if($db->getfirst("select * from iptable where voteip='$_SERVER[REMOTE_ADDR]'")){ echo "您已经投过票了,谢谢您的参与!"; } //这是投票项目列表页面: if(!$action){ echo "
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.