我已经使用HTML和JS在我的网站上开发了星级评定系统。它运行良好,但是当我尝试将该值发送到数据库时,它始终发送0。由于我是PHP新手,所以很难理解问题所在和位置。有时这可能是一个愚蠢的问题:/请有人在这里帮助我!
这是我的HTML代码:
<div class="form-group" id="rating-ability-wrapper">
<label class="control-label" for="rating">
<h2 align="center">Rate Our Service</h2>
<span class="field-label-info"></span>
<input type="hidden" id="selected_rating" name="selected_rating" value="" required="required">
</label>
<h2 class="bold rating-header" style="text-align: center;">
<span name="rate" value="rate" class="selected-rating">0</span><small> / 5</small>
</h2>
<div style="text-align: center;">
<button type="button" class="btnrating btn-default btn-lg" data-attr="1" id="rating-star-1" name="rating-star1" value="1">
<i class="fa fa-star" aria-hidden="true"></i>
</button>
<button type="button" class="btnrating btn-default btn-lg" data-attr="2" id="rating-star-2" name="rating-star2" value="2">
<i class="fa fa-star" aria-hidden="true"></i>
</button>
<button type="button" class="btnrating btn-default btn-lg" data-attr="3" id="rating-star-3" name="rating-star3" value="3">
<i class="fa fa-star" aria-hidden="true"></i>
</button>
<button type="button" class="btnrating btn-default btn-lg" data-attr="4" id="rating-star-4" name="rating-star4" value="4">
<i class="fa fa-star" aria-hidden="true"></i>
</button>
<button type="button" class="btnrating btn-default btn-lg" data-attr="5" id="rating-star-5" name="rating-star5" value="5">
<i class="fa fa-star" aria-hidden="true"></i>
</button>
</div>
</div><br><br><br><br><br>
JS代码:
jQuery(document).ready(function($){
$(".btnrating").on('click',(function(e) {
var previous_value = $("#selected_rating").val();
var selected_value = $(this).attr("data-attr");
$("#selected_rating").val(selected_value);
$(".selected-rating").empty();
$(".selected-rating").html(selected_value);
for (i = 1; i <= selected_value; ++i) {
$("#rating-star-"+i).toggleClass('btn-warning');
$("#rating-star-"+i).toggleClass('btn-default');
}
for (ix = 1; ix <= previous_value; ++ix) {
$("#rating-star-"+ix).toggleClass('btn-warning');
$("#rating-star-"+ix).toggleClass('btn-default');
}
}));
});
PHP代码:
<?php
require_once('dbh.inc_2.php');
$rate = '0';
if (isset($_POST['rating-star1'])) {
$rate = '1';
}
if (isset($_POST['rating-star2'])) {
$rate = '2';
}
if (isset($_POST['rating-star3'])) {
$rate = '3';
}
if (isset($_POST['rating-star4'])) {
$rate = '4';
}
if (isset($_POST['rating-star5'])) {
$rate = '5';
}
if (isset($_POST['button_post'])) {
$feedback=addslashes($_POST["feedback"]);
$f_author=$_POST["fAuthor"];
$f_date=$_POST["fDate"];
$rate_star = $rate;
if (empty($feedback)) {
$m = "Please enter your feedback";
header("location:../post_feedback.php?msg=$m");
exit();
}
else {
$sql ="INSERT INTO feedbacks(feedback_body, feedback_author, feedback_date, rating)VALUES('$feedback','$f_author','$f_date', '$rate_star')";
if (mysqli_query($conn, $sql))
{
$m= "Feedback Sent";
}
else
{
$m= "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
header("location:../post_feedback.php?msg=$m");
答案 0 :(得分:0)
将按钮的值发送到隐藏的输入中,然后提交表单。这将在此代码中起作用。 谢谢