0

So I'm trying to build a form for submitting input data and I want the values for each individual element to be stored as a Javascript variable so I can use SessionStorage to access them from another page (I don't want to send the data to a server since I can't pay for one and I don't intend on sending this to multiple users anyway). Problem is that no matter how I try to do it, the values are coming up as null. Here's my code (I changed the variable names because they're sensitive data, but I've checked the actual names and they're not reserved):

<form id="form_updates" method="post">
Input 1: <br>
<input type="text" name="input1" id="input1"><br>
Input 2: <br>
<input type="text" name="input2" id="input2"><br>
Input 3: <br>
<input type="text" name="input3" id="input3"><br>
<button onclick storeValues()>Update Form</button>
</form>

<script>
function storeValues(){
sessionStorage.setItem("ses1", "<?php echo $_POST['input1'] ?>");
sessionStorage.setItem("ses2", form_updates.elements[1].value);
sessionStorage.setItem("ses3", document.getElementById("input3").value);
}

So the code is a mess right now, I'm aware of that, I've been testing multiple solutions and none of them seem to work (once I figure out something that works I'll apply that consistently throughout the code). I've tried using an event listener on the button, and I've tried reading the form. I've tried all three of those methods to store the values inside and outside of storeValues(). Every time, it always evaluates to null. I've been researching this issue for days and I can't seem to figure this out. Please help!

Eclipse
  • 3
  • 3
  • 1
    _"I don't want to send the data to a server since I can't pay for one"_ not related to your problem but if you don't mind a steep-ish learning curve, you can deploy Firebase apps for free ~ https://firebase.google.com/ (disclaimer, I do not work for Google, I just like the platform) – Phil Jul 26 '19 at 01:08
  • 1
    Just a typo. You want ` – Phil Jul 26 '19 at 01:09
  • @Phil I tried adding type="button" to my button as suggested, but I still can't retrieve the values I'm submitting through my form. To clarify, if I were to input a value of "100" in the first textbox on my form, I want to store that value of 100 as input1, then I want storeValues to be able to access that value, then I want to store it as ses1 to use it for other files. I tried all three solutions for ses1, ses2, and ses3 even after adding type="button" and still nothing works, I'm getting null instead of 100. – Eclipse Jul 31 '19 at 00:59
  • Did you miss the other part of my comment where there's an equals sign and quotes for the `onclick` attribute, ie `onclick="storeValues()"`. If your code has changed and still doesn't work, please [update your question](https://stackoverflow.com/posts/57211852/edit) – Phil Jul 31 '19 at 01:02

0 Answers0