我正在使用 html 表单构建此评级系统:
<form action="rateProduct" method="post">
<fieldset class="rating">
<input type="radio" id="star1" name="rating" value="1"/><label for="star1"></label>
<input type="radio" id="star2" name="rating" value="2" /><label for="star2"></label>
<input type="radio" id="star3" name="rating" value="3" /><label for="star3"></label>
<input type="radio" id="star4" name="rating" value="4" /><label for="star4"></label>
<input type="radio" id="star5" name="rating" value="5" /><label for="star5"></label>
</fieldset>
<input name="ratingId" value="${selectedProduct.id}" type="hidden">
<input class="validate_rating" id="addRating" onclick="addedRating()" value="<fmt:message key='RateProduct'/>" type="submit">
<p id="voted" style="font-size:smaller;"></p>
</form>
当用户点击“添加我的评分”时,我想将用户评分发送到我的数据库。我想将所有数据特别发送到此数据库表:
rating
- rating_id
- rating_value
- product_id
- rating_date
表“rating”与表“product”形成联合表,表“product”创建了表名“product_has_ rating”:
product_has_rating
product_id
rating_id
我在 jsp
中使用 Controller Servlet
来转发数据,但我想了解如何将带有正确的product_id 的 rating_value 和 rating_date 发送到表Mysql 中的“评级”。我走在正确的道路上吗?
Controller Servlet:
// if rateProduct action is called
} else if (userPath.equals("/rateProduct")) {
// get input from request
String productId = request.getParameter("productId");
String rating = request.getParameter("rating_value");
userPath = "/product";
最佳答案
<form action="rateProduct" method="post" id="formToSend">
<fieldset class="rating">
<input type="radio" id="star1" name="rating" value="1" onchange="javascript:sendIt();"/><label for="star1"></label>
<input type="radio" id="star2" name="rating" value="2" onchange="javascript:sendIt();" /><label for="star2"></label>
<input type="radio" id="star3" name="rating" value="3" onchange="javascript:sendIt();" /><label for="star3"></label>
<input type="radio" id="star4" name="rating" value="4" onchange="javascript:sendIt();" /><label for="star4"></label>
<input type="radio" id="star5" name="rating" value="5" onchange="javascript:sendIt();" /><label for="star5"></label>
</fieldset>
<input name="ratingId" value="${selectedProduct.id}" type="hidden">
<input class="validate_rating" id="addRating" onclick="addedRating()" value="<fmt:message key='RateProduct'/>" type="submit">
<p id="voted" style="font-size:smaller;"></p>
</form>
<script>
function sendIt() {
document.getElementById("formToSend").submit();
}
</script>
关于mysql - 如何使用Controller Servlet将表单数据发送到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29508241/