I have a JSP file of a shopping cart and to evaluate which servlet to use depending on the button action, with JavaScript I used a method to attach it, but when I click the button nothing happens.
This code is part of the head and has a java code where I evaluate the role of the user through a session:
<head>
<title>Runner Store</title>
<link rel="stylesheet" href="css/<%=css%>">
<%
String js="";
if (session.getAttribute("rol") != null) {
int rol = (Integer) session.getAttribute("rol");
if (rol==1){
js = (String)session.getAttribute("jsuser");
} else {
js = (String)session.getAttribute("jsadmin");
}
}else{
js = "main.js";
}
%>
<script src="js/<%=js%>"></script>
</head>
Well, the validation does what is expected, give the src the corresponding js, but here the problem, I have a simple function to adhere to the form the action and the method, as well as a hidden input that contains the value of the id of the product that I am going to add to the cart:
<% String funcag = "agregar()"; %>
<input type="button" class="boton" id="sendagbtn" value="Añadir al carrito" onclick="<%=funcag%>">
<input type="hidden" name="sendag" value="<%=a.getId()%>">; //esto es de un for each que me trae datos del producto.
and this is the function of js:
function agregar(){
var serv = '/RunnerStoreApp/AddCarrito';
var met = 'POST';
document.getElementById("sendagbtn").value = "Añadido al carrito";
document.getElementById("sendagbtn").disabled = true;
document.forms.formaction = serv;
document.forms.formmethod = met;
}
And here is where I arrive, that when I click on the button, it does not run.
Note: I'm a novice with JavaScript.