<%--
  Created by IntelliJ IDEA.
  User: lc
  Date: 2023-04-01
  Time: 上午 11:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js"></script>
</head>
<body>


<form action="#">
    <input id="book_id" name="book_id" value="${book.book_id}" />   <br />
    <input id="book_name" name="book_name" value="${book.book_name}" />  <br />
    <input id="price" name="price" value="${book.price}" />  <br />
    <input id="stock" name="stock" value="${book.stock}" />  <br />
    <input  type="button" onclick="update()" value=" 提交修改 ">
</form>


<script>


    function update(){

        var book_id = document.querySelector("#book_id").value
        var book_name = document.querySelector("#book_name").value
        var price = document.querySelector("#price").value
        var stock = document.querySelector("#stock").value
        var formData = {
            book_id : book_id,
            book_name : book_name,
            price : price,
            stock : stock,
        }
        var xmlHttpRequest = new XMLHttpRequest();
        xmlHttpRequest.open(
            "PUT",
            "http://localhost:8080/day03_war_exploded/book/"
        )
        xmlHttpRequest.setRequestHeader(
            "Content-Type", "application/x-www-form-urlencoded" )
        xmlHttpRequest.onreadystatechange = function(){
            if ( xmlHttpRequest.status == 200
                && xmlHttpRequest.readyState == 4 ) {
                window.location.href = "<%=request.getContextPath()%>/success.jsp"
            }
        }
        xmlHttpRequest.send( Qs.stringify(formData) )
    }

</script>



</body>
</html>
