我正在尝试进行在线投票 目前我要做的是从一个表单在不同页面上获取输入,然后使用for循环显示下一页上的所有民意调查。每个投票表单可以使用广播或复选框,具体取决于创建者。我提交个人民意调查时遇到问题。由于表单是使用for循环创建的,因此每个表单都作为具有相同名称的提交按钮 另一个主要问题是,如果创建者选择了轮询复选框(即允许多个选项),我希望在数据库中单独输入每个选项。
有没有办法用AJAX解决这个问题?
`
$pollqry2 = "SELECT * FROM pollq ";
$PollId= $PTitle= $PDesc= $POption= $POption1= $POption2= $POption3= $POption4= $POption5= $POption6= $POption7= $Target_Aud= $SubGroup= $PollType= $CloseDate= $PNotify= $PComment= $PostAs= array();
$j=0;
$i=0;
$pollres = mysqli_query($con,$pollqry2);
while($poll = mysqli_fetch_assoc($pollres)){
$PollId[$i]= $poll['PollId'];
$PTitle[$i]= $poll['PTitle'];
$PDesc[$i]= $poll['PDesc'];
$POption[$i]= $poll['POption'];
$POption1[$i]= $poll['POption1'];
$POption2[$i]= $poll['POption2'];
$POption3[$i]= $poll['POption3'];
$POption4[$i]= $poll['POption4'];
$POption5[$i]= $poll['POption5'];
$POption6[$i]= $poll['POption6'];
$POption7[$i]= $poll['POption7'];
$Target_Aud[$i]= $poll['Target_Aud'];
$SubGroup[$i]= $poll['SubGroup'];
$PollType[$i]= $poll['PollType'];
$CloseDate[$i]= date('j F Y',strtotime($poll['CloseDate']));
$PNotify[$i]= $poll['PNotify'];
$PComment[$i]= $poll['PComment'];
$PostAs[$i]= $poll['PostAs'];
$i++;
}
?>
<!-- Main content -->
<section class="newsfeed">
<div class="box box-solid box-info">
<table class="disscussio" width="750">
<tr>
<td>
<font size="6"> Polls at [Name of appartment]</font>
<br>
<font size="3">
<a href="#">Active Polls: <span class="badge"> 5</span></a>
<a href="#">All Poll</a>
</font><br>
</td>
</tr>
<table class="news">
<?php
function dateDiff($start, $end)
{
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$diff = $end_ts - $start_ts;
return round($diff / 86400);
}
for($j=0; $j<$i;$j++){
$date1 = date('Y-m-d', strtotime($CloseDate[$j]));
$date2 = date('y-m-d',strtotime("now"));
$date3 = dateDiff($date2, $date1);
if($date3<0){$date4 = "Closed";}
elseif($date3 == 0){$date4 = "Today";}
else{$date4 = $CloseDate[$j];}
?>
<tr>
<td width="748" class="dispad">
<h4><b><?php echo $PTitle[$j]; ?></b></h4>
<pre><?php echo $PDesc[$j]; ?></pre>
<form method="post">
<div class="poll-prog">
<div class="row">
<div class="pull-left col-md-1">
<input class="minimal" type="<?php echo $POption[$j]; ?>" name="Pop<?php if($POption[$j]=='checkbox') echo "1"; ?>" value="<?php echo $POption1[$j]; ?>">
</div>
<div class="col-md-5 progress" style="width:400px; padding:0;background:rgba(148, 215, 250, 0.37)">
<div class="progress-bar progress-bar-success progress-bar-striped" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 10%">
<span>
<label>
<?php echo $POption1[$j]; ?>
</label>
</span>
</div>
</div>
</div>
<br>
<div class="row">
<div class="pull-left col-md-1">
<input class="minimal" type="<?php echo $POption[$j]; ?>" name="Pop<?php if($POption[$j]=='checkbox') echo "2"; ?>" value="<?php echo $POption2[$j]; ?>">
</div>
<div class="col-md-5 progress" style="width:400px; padding:0;background:rgba(148, 215, 250, 0.37)">
<div class="progress-bar progress-bar-info progress-bar-striped" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 80%">
<span>
<label>
<?php echo $POption2[$j]; ?>
</label>
</span>
</div>
</div>
</div>
<br>
<?php if(!empty($POption3[$j])){ ?>
<div class="row">
<div class="pull-left col-md-1">
<input class="minimal" type="<?php echo $POption[$j]; ?>" name="Pop<?php if($POption[$j]=='checkbox') echo "3"; ?>" value="<?php echo $POption3[$j]; ?>">
</div>
<div class="col-md-5 progress" style="width:400px; padding:0;background:rgba(148, 215, 250, 0.37)">
<div class="progress-bar progress-bar-warning progress-bar-striped" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 20%">
<span>
<label>
<?php echo $POption3[$j]; ?>
</label>
</span>
</div>
</div>
</div>
<br>
<?php } ?>
<?php if(!empty($POption4[$j])){ ?>
<div class="row">
<div class="pull-left col-md-1">
<input class="minimal" type="<?php echo $POption[$j]; ?>" name="Pop<?php if($POption[$j]=='checkbox') echo "4"; ?>" value="<?php echo $POption1[$j]; ?>">
</div>
<div class="col-md-5 progress" style="width:400px; padding:0;background:rgba(148, 215, 250, 0.37)">
<div class="progress-bar progress-bar-danger progress-bar-striped" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 50%">
<span>
<label>
<?php echo $POption4[$j]; ?>
</label>
</span>
</div>
</div>
</div>
<br>
<?php } ?>
<?php if(!empty($POption5[$j])){ ?>
<div class="row">
<div class="pull-left col-md-1">
<input class="minimal" type="<?php echo $POption[$j]; ?>" name="Pop<?php if($POption[$j]=='checkbox') echo "5"; ?>" value="<?php echo $POption5[$j]; ?>">
</div>
<div class="col-md-5 progress" style="width:400px; padding:0;background:rgba(148, 215, 250, 0.37)">
<div class="progress-bar progress-bar-primary progress-bar-striped" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 60%">
<span>
<label>
<?php echo $POption5[$j]; ?>
</label>
</span>
</div>
</div>
</div>
<br>
<?php } ?>
<?php if(!empty($POption6[$j])){ ?>
<div class="row">
<div class="pull-left col-md-1">
<input class="minimal" type="<?php echo $POption[$j]; ?>" name="Pop<?php if($POption[$j]=='checkbox') echo "6"; ?>" value="<?php echo $POption6[$j]; ?>">
</div>
<div class="col-md-5 progress" style="width:400px; padding:0;background:rgba(148, 215, 250, 0.37)">
<div class="progress-bar bg-orange progress-bar-striped" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 40%">
<span>
<label>
<?php echo $POption6[$j]; ?>
</label>
</span>
</div>
</div>
</div>
<br>
<?php } ?>
<?php if(!empty($POption7[$j])){ ?>
<div class="row">
<div class="pull-left col-md-1">
<input class="minimal" type="<?php echo $POption[$j]; ?>" name="Pop<?php if($POption[$j]=='checkbox') echo "7"; ?>" value="<?php echo $POption7[$j]; ?>">
</div>
<div class="col-md-5 progress" style="width:400px; padding:0;background:rgba(148, 215, 250, 0.37)">
<div class="progress-bar bg-purple progress-bar-striped" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 30%">
<span>
<label>
<?php echo $POption7[$j]; ?>
</label>
</span>
</div>
</div>
</div>
<br>
<?php } ?>
</div>
<div class="poll-btn"><center>
<input type="submit" class="btn btn-success btn-flat" name="PAsubmit<?php echo $j ?>" value="Vote" ></center>
</div>
</form>
<br>
By 'sender Name' <br>
<font>Closing Date : <b><?php echo $date4; ?></b></font>
<p style="float:right">
VOTES <span class="badge">5</span>
|
COMMENTS <span class="badge">5</span>
<a href="">
<img src="plugins/delete.png" height="15" widht="15" align="right">
</a>
</p>
<br>
<font>Open to <?php echo $Target_Aud[$j] ?></font>
</td>
</tr>
<?php
}
?>
</table>
</table>
</div>
</section><!-- /.content -->
</div><!-- /.content-wrapper -->
`
答案 0 :(得分:0)
您可以在所有表单中添加一个类,并使用类提交特定表单。
像这样: $('.classname').click(function(){
$(this).submit();
});
使用复选框名称作为数组并在表单提交后使用。使用foreach或for循环将其存储在数据库中。
答案 1 :(得分:0)
这就是我所做的 -
function getVote(clicked_id) {
var val = [];
var userp=<?php echo $_SESSION['newdata'] ;?>;
var div_id = 'poll'+clicked_id;
$('#'+div_id+' input:checked').each(function(i){
val[i] = $(this).val();
});
if(typeof val != "undefined" && val != null && val.length > 0){
//ajax code to submit form
}else{
alert("Please Select Choice First");
}
}
我在提交按钮上使用了onClick函数来获取其id,以获取相应的div的id。之后我在该div中找到了已检查输入的数量,将它们的值放在一个数组中并将其传递给Php以使用ajax提交。