The processing of multiple selections by PHP and JavaScript

Source: Internet
Author: User
Tags class definition count key mysql php and query reset mysql database
JavaScript we often have to make a number of choices for users to process, such as allowing users to select a list of items to delete the selected items and so on. Here's an example of how PHP and JavaScript deal with multiple choices, respectively. What we do today is a voting system that polls the MySQL database itemtable, and can only vote two votes per individual IP.

The table itemtable is established through the following MySQL statement:

CREATE TABLE ' itemtable ' (
' ID ' TINYINT (4) Not NULL auto_increment,
' Name ' VARCHAR not NULL,
' Votes ' SMALLINT (6) Not NULL,
PRIMARY KEY (' id ')
);

Where the field "name" is the list item name, "votes" is the resulting number of votes. We also want to create a table "voteiptable" to record the IP of the voting user:

CREATE TABLE ' voteiptable ' (
' ID ' SMALLINT (6) Not NULL,
' Voteip ' VARCHAR not NULL,
PRIMARY KEY (' id ')
);

Below we write the file "multivote.php", today we want to use a database class file "dbclass.php", please refer to the flops "MySQL database class definition" for details:

<HTML>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> ask you to vote </title>
<style type= "Text/css" >
<!--
Body, td{
Font-family: "The song Body";
font-size:12px;
}
-->
</style>
<body>
?
Contains database class files:
Include_once ("dbclass.php")
Check to see if the IP has been voted on:
if ($db->getfirst ("select * from iptable where voteip= ' $_server[remote_addr]") {
echo "You have voted, thank you for your participation!" ";
}

This is the list of voting items page:
if (! $action) {
echo "<table width=200 border=0 cellpadding=2 cellspacing=1>";
echo "<form action= ' method= ' post ' name= ' Voteform ' >";

We give each check box the name: Check1, Check2, Check3 、......, their value is the ID of the item,
Then we will be more convenient to deal with:
$myitems = $db->query ("SELECT * from itemtable");
$itemNo = 0;
while ($myitem = $db->getarray ($myitems)) {
$itemNo + +;
echo "<tr><td nowrap style= ' Cursor:default ' > $myitem [name]</td></tr>";
}

Echo ' <tr><td><input name= "action" type= "hidden" value= "vote" ><br><input name= "Submit" Type= "Submit" value= "vote" > <input name= "reset" type= "reset" value= "reset" ></td></tr>;
echo "\n</form>\n </table>";

?>
<!--now write JavaScript programs to initially validate user forms, allowing users to select only two items-->
<script langvage= "JavaScript" >
<!--
function Checkform (MyForm) {
checkedcount=0;
Loop check checkbox is checked, if, Checkedcount plus 1:
For (i=1;i<=<?php echo $itemNo? >;i++) {
Mycheck=eval ("Myform.check" +i); MyCheck is a check box object
if (mycheck.checked==true) {
checkedcount++;
}
}
if (checkedcount==2) {//If only two check boxes are selected, submit;
return true;
}else{//Otherwise issue a warning:
Alert ("Sorry, you can only cast two items!") ");
return false;
}
}
-->
</script>
?
}
This is the page that handles the user vote.
else if ($action = "vote") {

It is not safe to test data with JavaScript on the client side, but it can reduce the server burden and not waste user time.
It is often necessary to authenticate again on the server.

$itemcount = $db->getfirst ("SELECT count (*) as Count from ItemTable"); Total number of projects achieved
$checkarray =array ();
For ($i =1 $i <= $itemcount ["Count"]; $i + +) {//and JavaScript processing is not a very similar process.
$mycheck = "check$i"; $mycheck =$ $mycheck;
if ($mycheck && $db->getfirst ("select * from itemtable where id= ' $mycheck ')" {
The reason for adding $db->getfirst ("select * from itemtable where id= ' $mycheck") is to prevent the cast item ID from not exist
$checkarray []= $mycheck; If this item is selected, $checkarray add an item, the value is the selected ID
}
}
if (count ($checkarray) ==2) {//If the number of items $checkarray is 2, the corresponding ticket number is 1:
for ($i =0; $i <count ($checkarray); $i + +) {
$db->query ("Update itemtable set votes=votes+1 where id= ' $checkarray [$i]");
///The IP of the voting user is recorded in the database:
$db->query ("INSERT into iptable (VOTEIP) VALUES (' $_server[remote_addr]");
echo "Vote for success, thank you for your participation!" ";
}else{//otherwise:
echo "Sorry, you can only cast two items!" ";
}
}
?>
</body>
</HTML>

We can see that the client's JavaScript and server-side PHP have a lot in common with the processing of multiple choices, and of course they are different. This is a more classic multi-option processing program, if you do not qualify the user's choice of words is simpler.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.