Home > php教程 > PHP开发 > body text

Multiple selection processing with PHP and JavaScript

高洛峰
Release: 2016-11-25 09:49:52
Original
1368 people have browsed it

 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".




请您投票


getfirst("select * from iptable where voteip='$_SERVER[REMOTE_ADDR]'")){ echo "您已经投过票了,谢谢您的参与!"; } //这是投票项目列表页面: if(!$action){ echo ""; echo ""; //我们给每个复选框起这样的名字:check1、check2、check3、……,它们的值分别是项目的id, //这样到时候我们处理起来就比较方便了: $myitems=$db->query("select * from itemtable"); $itemNo=0; while($myitem=$db->getarray($myitems)){ $itemNo++; echo " \n"; } echo ' '; echo "\n\n
$myitem[name]

"; ?> 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;$iquery("update itemtable set votes=votes+1 where id='$checkarray[$i]'"); } //把投票用户的IP记入数据库: $db->query("insert into iptable (voteip) values ('$_SERVER[REMOTE_ADDR]')"); echo "投票成功,谢谢您的参与!"; }else{ //否则: echo "对不起,您只能投两个项目!"; } } ?>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!