Javascript 创建一个会话以在投票中不再投票
我使用jQueryAjax和JSON创建了simple。我想知道如何创建一个会话,这样用户将无法再次投票。下面是我的代码 我不熟悉会话和jQuery。请告诉我如何完成我的任务 JavaScriptJavascript 创建一个会话以在投票中不再投票,javascript,php,jquery,session,vote,Javascript,Php,Jquery,Session,Vote,我使用jQueryAjax和JSON创建了simple。我想知道如何创建一个会话,这样用户将无法再次投票。下面是我的代码 我不熟悉会话和jQuery。请告诉我如何完成我的任务 JavaScript <script> $(document).ready(function(){ $("#poll").click(function(){ var count = ''; if (document.getElement
<script>
$(document).ready(function(){
$("#poll").click(function(){
var count = '';
if (document.getElementById("vote1").checked) {
count = 0;
}
if (document.getElementById("vote2").checked) {
count = 1;
}
var jsonV=
{
"vote": count
};
$.ajax({
type : "POST",
url : "poll_vote.php",
data : jsonV,
dataType: "json",
success : function (responseText){
console.log("Shit is working "+responseText);
$("#result").html(responseText.vote);
},
complete : function(){
$("#poll").slideUp();
},
error : function(error,responseText){
// alert("Server not Responding. Sorry for the inconvenience caused. Please Try again Later");
console.log(error);
$("#result").html(error+ responseText);
alert(count);
}
});
});
});
</script>
$(文档).ready(函数(){
$(“#poll”)。单击(函数(){
var计数=“”;
if(document.getElementById(“vote1”).已选中){
计数=0;
}
if(document.getElementById(“vote2”).已选中){
计数=1;
}
var jsonV=
{
“投票”:计数
};
$.ajax({
类型:“POST”,
url:“poll_vote.php”,
资料来源:jsonV,
数据类型:“json”,
成功:函数(responseText){
日志(“狗屎正在工作”+响应文本);
$(“#结果”).html(responseText.vote);
},
完成:函数(){
$(“#poll”).slideUp();
},
错误:函数(错误,响应文本){
//警报(“服务器没有响应。对由此造成的不便深表歉意。请稍后再试”);
console.log(错误);
$(“#结果”).html(错误+响应文本);
警报(计数);
}
});
});
});
PHP
<?php
$vote = $_REQUEST['vote'];
$filename = "poll_result.txt";
$content = file($filename);
// $decode = json_decode($encode);
$array = explode("||", $content[0]);
$male = $array[0];
$female = $array[1];
if ($vote == 0) {
$male = $male + 1;
}
if ($vote == 1) {
$female = $female + 1;
}
$insertvote = $male."||".$female;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
$table = (
"<h2>Results:</h2>
<table>
<tr>
<td> Male :</td>
<td>
<img src='poll.gif'
width= ".(100*round($male/($female+$male),2)).
"height='20'>".
(100*round($male/($female+$male),2))." %" .
"
</td>
</tr>
<tr>
<td> Female :</td>
<td>
<img src='poll.gif'
width=". (100*round($female/($female+$male),2)) .
"
height='20'>".
(100*round($female/($female+$male),2))." %" ."
</td>
</tr>
</table>");
$list = array('vote' => $table);
$encode = json_encode($list);
echo $encode;
?>
HTML
<div id= "poll">
<h3> What is your Gender? </h3>
<form>
Male :
<input type = "radio" name = "vote" id="vote1" >
<br>
Female :
<input type = "radio" name = "vote" id="vote2" >
</form>
</div>
<p><div id= "result"></div>
</body>
你的性别是什么?
男:
女性:
在PHP代码顶部启动一个会话,然后检查是否有所需的会话值,例如:
<?php
session_start();
if(isset($_SESSION['voted']){
$list = array('vote' => 'You have already voted!');
$encode = json_encode($list);
echo $encode;
exit;
//or, shorter: die(json_encode(array('vote' => 'You have already voted!')));
}
$_SESSION['voted'] = 1;
//rest of your code here
Cookies是一个更好的选择。使用session\u start()
function。并设置会话变量。如$\u session[“login”]=“green”代码>@LShetty你能告诉我怎么做吗?我做了。您能告诉我如何在COOKIE中执行此操作吗?同样,只需将$\u会话替换为$\u COOKIE即可。