I'm trying to set cookies using Javascript. This code had been working up until recently, when I started getting null
for the value of the cookies. I'm really not sure what I could've changed to cause this. Here is my HTML
:
<div class="w3-container w3-center" style="align-content:center;">
<br>
<br>
<h1 class="w3-center w3-jumbo">Find a Fruit Here</h1>
<form method="post" name="input-form" id="input-form" class="w3-center w3-mobile">
<h4 class="w3-center" style="max-width:1000px;align-content:center;">
<label>Address</label>
</h4>
<input id="input-address" style="max-width:300px;margin-left:auto;margin-right:auto" class="w3-center w3-input w3-border" name="Address" placeholder="123 45th Street, City, State" required>
<h4 class="w3-center" style="max-width:1000px;">
<label>Fruits(at least one)</label>
</h4>
<input id="input-items" style="max-width:300px;margin-left:auto;margin-right:auto" class="w3-center w3-input w3-border" name="Items" placeholder="Apples, bananas, etc." required>
<h4>
<button style="max-width:250px;margin-left:auto;margin-right:auto" class="w3-button w3-black w3-round w3-hover-light-grey w3-center" type="submit" onclick="cookies()">
Search
</button>
</h4>
</form>
<br>
</div>
And this is my Javascript. I've added a logging line to test, but that never gets printed to the console, so I figured I might have a problem calling the function.
<script>
function cookies() {
document.getElementById("input-form").submit();
document.cookie = "address=" + document.getElementById("input-address").value.replaceAll(",", "/");
document.cookie = "items=" + document.getElementById("input-items").value.replaceAll(",", "/");
}
</script>
Any help is really appreciated!! I'm at a loss.
EDIT: I'm accessing my cookies in a Java program using Javalin, and that's where I'm seeing that they're null. I'm rendering the document in Javalin and then trying to access the cookies, then getting a NullPointerException. Here is the Java code:
app.get("/search", ctx -> ctx.render("/donation-find.html"));
app.post("/search", ctx -> {
ctx.render("/donation-find.html");
address = ctx.cookie("address").replaceAll("/", ",");
String[] temp = ctx.cookie("items").split("/");
}
Just for clarification purposes, I ended up solving the problem by using an entirely different method of transferring data. Instead of using cookies, I just used the Javalin built-in method formParam(key)
to access the data posted to the form