【问题标题】:My poll results for my coloured bars don't work我的彩色条的投票结果不起作用
【发布时间】:2020-06-08 05:54:39
【问题描述】:

我正在尝试使用两个粉红色条进行民意调查,以显示结果的百分比。我制作了百分比条形图像,尽管当我使用民意调查时,它们的大小保持不变,并且根据它们的百分比不会变得更小/更大。我应该如何更改代码以使 % 条改变它们的长度?

HMTL 代码:

<html>
<head>
<script>
function getVote(int) {
  var xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("poll").innerHTML=this.responseText;
    }
  }
  xmlhttp.open("GET","poll_vote.php?vote="+int,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<div id="poll">
<h3>Do you like PHP and AJAX so far?</h3>
<form>
Yes: <input type="radio" name="vote" value="0" onclick="getVote(this.value)"><br>
No: <input type="radio" name="vote" value="1" onclick="getVote(this.value)">
</form>
</div>

</body>
</html>

PHP 文件:

<?php
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0) {
  $yes = $yes + 1;
}
if ($vote == 1) {
  $no = $no + 1;
}

//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>

<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td><img src="images/poll.png"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td><img src="images/poll.png"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>

【问题讨论】:

标签: javascript php html jquery css


【解决方案1】:

您不应使用图像来指示进度。使用 &lt;progress&gt; 或其他带有 CSS 的 HTML 元素。

我已将您的 poll_vote.php 文件更改为使用 &lt;progress&gt; 元素。

<?php
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0) {
  $yes = $yes + 1;
}
if ($vote == 1) {
  $no = $no + 1;
}

//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);

$yesProgress = 100*round($yes/($no+$yes),2);
$noProgress = 100*round($no/($no+$yes),2);
?>

<h2>Result:</h2>
<table>
    <tr>
        <td>Yes:</td>

        <td>
        <progress id="file" max="100" value="<?= $yesProgress ?>">
            <?= $yesProgress ?>
        </progress>
        <?= $yesProgress ?>%
        </td>
    </tr>
    <tr>
        <td>No:</td>
        <td>
        <progress id="file" max="100" value="<?= $noProgress ?>">
            <?= $noProgress ?>
        </progress>
        <?= $noProgress ?>%
        </td>
    </tr>
</table>

参考以下链接:

HTML Element

Bootstrap 4 Progress Bars

【讨论】:

  • 哇,谢谢!那确实奏效了。再次抱歉,我如何添加一个随着 % 增加而增加的百分比标签?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 2013-02-23
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
相关资源
最近更新 更多