我正在尝试建模一个网页,该网页允许用户对某些项目进行评级并在后台存储这些评级。我有一张这样的桌子 项目ID |标题|评级。在评级栏中,我有条件地在某些列中选择标签,否则会有一些文字。我在select上添加了一个onchange事件,当option的值被更改时触发了javascript函数,但是我想提取select_ value更改的行的project_id feild的值。我是javascript的新手所以我找不到任何可以解决我的问题的东西,我需要那个项目id值,因为我需要将这个值存储在我的数据库中,我已经通过Ajax Calls工作了。这是我的下表: 代码:
<?php if($row['status']==='complete' && $result2->num_rows==0 )
{ ?>
<select id="rating select" onchange="rating()" class="form-control" name="projectrating">
<option selected>0</option>
<option value="1" >1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<?php } ?>
<?php if($row['status']==='complete' && $result2->num_rows > 0 )
{ $row2=$result2->fetch_assoc();
$rating=$row2['rating'];
echo "You have already rated this Project!,You have given the project a rating of $rating";
} ?>
<?php
if($row['status']!=='complete') { ?>
<?php echo "Ratings only for complete projects" ?>
<?php } ?>
</td>
<?php } ?>
Javascript :(我需要填充变量参数而没有值,所以我可以使用Ajax将其发送到php脚本。)
<script>
function rating()
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4) {
if(this.status===400){
alert("There was a problem while giving the rating");
}
if(this.status===200)
{
alert("Rating given succesfully");
window.location.reload(true);
}
}
};
xhttp.open("POST", "rating.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var param=
xhttp.send(param);
}