2015-04-08 72 views
0

我建立這個評級系統與HTML表單:如何使用Controller Servlet將表單數據發送到數據庫?

<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 

表「評級」形成與表「產品」,這創造了一個表名「product_has_rating」聯合表:

product_has_rating 
product_id 
rating_id 

我在jsp中使用Controller Servlet來轉發數據,但我想知道如何發送rating_value和rating_date以及正確的product_id到Mysql中的表「rating」。我在好路上嗎?

控制器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"; 

回答

0
<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> 
相關問題