mysql - 如何使用Controller Servlet将表单数据发送到数据库?

标签 mysql jsp forms

我正在使用 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/

相关文章:

php - 3gp 罚款未转移到数据库

java - 在屏幕上刷新数据表中的数据(jsp)

javascript - Formik + React Form 中的下拉列表和值未更新

php - 如何使用添加更多选项在 Drupal 7 Ajax 表单中保留和检索图像详细信息?

php - 第二次尝试限制 mySQL 和 PHP 中的搜索

mysql - 条件顺序是否会影响 MySQL 的性能?

java - 如何更改jbpm 6.5的kie工作台前端?

javascript - HTML/Javascript : Send field only if it changed

mysql - 谷歌大查询中行之间的时间差异

jsp - Struts2 禁用 <s :textfield> using some expression