-1

I was trying to send form data from one webpage to another using this: Passing data from one web page to another

I am pasting the javascript code of the function i created:

function store(){
            window.localStorage.setItem("name",document.getElementById("name").value);
            window.localStorage.setItem("email",document.getElementById("email").value);
            window.localStorage.setItem("tele",document.getElementById("tele").value);
            window.localStorage.setItem("gender",document.getElementById("gender").value);
            window.localStorage.setItem("comment",document.getElementById("comments").value);
            window.location.href = "contact.html";
        }

But the Problem is that the window.location.href is not working and the webpage is not redirected.

I have seen the console for errors but there aren't any.

Can anyone tell Where is the fault?

EDIT 1: Validate function:

function validate(){
            var comment = document.getElementById("comments").value;
            var gender = document.getElementById("gender").value;
            var len = comment.length;
            if (len > 100)
            {
                alert("Reduce the length of the comment less than 100 characters");
            }
            else if (gender == "Select Gender")
            {
                alert("Please Select a gender");
            }
            else {
                store();
            }
        }

Form's html:

<form class="form-horizontal" role="form">

Submit Button:

<input type="submit" id="submit" value="Submit" onclick="validate()">
Snake Eyes
  • 325
  • 5
  • 18
  • So is your problem with passing form data or with redirecting to the other page? How are you calling this function? If it's in a form's submit handler, make sure you prevent the default form submission. – Barmar May 31 '16 at 21:43
  • I am using `onclick` handler on the submit button to call a function `validate()` to validate the form data. That `validate()` function after validating calls this function `store()` – Snake Eyes May 31 '16 at 21:46
  • Does `validate` prevent the default action of the submit button? – Barmar May 31 '16 at 21:46
  • @Barmar Can u suggest any other method to send form data without using backend mechanisms? – Snake Eyes May 31 '16 at 21:47
  • 1
    Why do you need another method? There's nothing wrong with using localStorage. You just need to fix the redirect problem, by disabling the form's default submission. – Barmar May 31 '16 at 21:51

1 Answers1

0

Have a look at this plunker:

https://plnkr.co/edit/P4XqhYciFxZeYlRbWXtd?p=preview

<form class="form-horizontal" role="form">
      <textarea rows="4" cols="50" id="comments" placeholder="Enter comments"></textarea>
      <br />
      <select id="gender">
        <option value="Select Gender">Select Gender</option>
        <option value="male">male</option>
        <option value="female">female</option>
      </select>
      <br />
      <button type="button" id="submit" onclick="validate()">Go</button>
    </form>

I have updated the plunker. The page is redirecting and you can see that the values are saved in the local storage. It works fine.

Perhaps you had a problem because of your input submit, I used a button instead.

samir benzenine
  • 478
  • 4
  • 11