国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP開發(fā) Multiple selection processing with PHP and JavaScript

Multiple selection processing with PHP and JavaScript

Nov 25, 2016 am 09:49 AM

 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>
<?
//包含數(shù)據(jù)庫類文件:
include_once("dbclass.php")//檢查該IP是否已經(jīng)投過票了:
if($db->getfirst("select * from iptable where voteip=&#39;$_SERVER[REMOTE_ADDR]&#39;")){
echo "您已經(jīng)投過票了,謝謝您的參與!";
}
//這是投票項目列表頁面:
if(!$action){
echo "<table width=200 border=0 cellpadding=2 cellspacing=1>";
echo "<form action=&#39;&#39; method=&#39;post&#39; name=&#39;voteform&#39; onsubmit=&#39;return checkform(this)&#39;>";
//我們給每個復選框起這樣的名字:check1、check2、check3、……,它們的值分別是項目的id,
//這樣到時候我們處理起來就比較方便了:
$myitems=$db->query("select * from itemtable");
$itemNo=0;
while($myitem=$db->getarray($myitems)){
$itemNo++;
echo "<tr><td nowrap style=&#39;cursor:default&#39; onclick=&#39;voteform.check$itemNo.click()&#39;><input name=&#39;check$itemNo&#39; type=&#39;checkbox&#39; value=&#39;$myitem[id]&#39;>$myitem[name]</td></tr> \n";
}
echo &#39;<tr><td><input name="action" type="hidden" value="vote"><BR><input name="submit" type="submit" value="投票"> <input name="reset" type="reset" value="重置"></td></tr> &#39;;
echo "\n</form>\n </table>";
?>
<!-- 現(xiàn)在編寫JavaScript程序初步驗證用戶表單,只允許用戶選擇兩個項目 -->
<script langvage="JavaScript">
<!--
function checkform(myform){
checkedcount=0;
//循環(huán)檢測復選框是否選中,若是,checkedcount加1:
for(i=1;i<=<?php echo $itemNo ?>;i++){
mycheck=eval("myform.check"+i); //mycheck是復選框?qū)ο?
if(mycheck.checked==true){
checkedcount++;
}
}
if(checkedcount==2){ //如果只有兩個復選框選中,就提交;
return true;
}else{ //否則發(fā)出警告:
alert("對不起,您只能投兩個項目!");
return false;
}
}
-->
</script>
<?
}
//這是處理用戶投票的頁面
else if($action="vote"){
//在客戶端用javascript檢驗數(shù)據(jù)是不安全的,但是可以減輕服務器負擔和不用浪費用戶時間。
//在服務器再次進行驗證經(jīng)常是必要的。
$itemcount=$db->getfirst("select count(*) as count from itemtable"); //取得項目總數(shù)
$checkarray=array();
for($i=1;$i<=$itemcount["count"];$i++){ //和javascript處理的過程是不是很像呢
$mycheck="check$i"; $mycheck=$$mycheck;
if($mycheck && $db->getfirst("select * from itemtable where id=&#39;$mycheck&#39;")){
//之所以加上$db->getfirst("select * from itemtable where id=&#39;$mycheck&#39;")是為防止所投項目id不存在
$checkarray[]=$mycheck; //如果這一項被選中,$checkarray增加一項,值為所選id
}
}
if(count($checkarray)==2){ //如果$checkarray的項數(shù)是2,則相應的票數(shù)加1:
for($i=0;$i<count($checkarray);$i++){
$db->query("update itemtable set votes=votes+1 where id=&#39;$checkarray[$i]&#39;");
} //把投票用戶的IP記入數(shù)據(jù)庫:
$db->query("insert into iptable (voteip) values (&#39;$_SERVER[REMOTE_ADDR]&#39;)");
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.

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)