Ofir Ohayon
Ofir Ohayon

Reputation: 367

how to use localstorage on form submit

I Have the next form -

         <form name="local_storage_form" method="post" autocomplete="on" data-theme="e" style="padding: 10px;">


         <label for="id">identity card number</label><input type="text" name="id" id="id" placeholder="identity card number..." data-theme="e" required class="localStore">
         <label for="hphone">Home Phone Number</label><input type="tel" name="hphone" id="hphone" placeholder="Home Phone Number..." data-theme="e" class="localStore">
         <label for="mphone">Mobile Phone Number</label><input type="text" name="mphone" id="mphone" placeholder="Mobile Phone Number..." data-theme="e" required class="localStore" oninvalid="setCustomValidity('Invaild Phone Number (05********)')">
         <label for="bdate">Birth Date</label><input type="date" name="bdate" placeholder="Birth Date..." id="bdate" data-theme="e" required class="localStore">
         <label for="email">Email Address</label><input type="email" name="email" id="email" autocomplete="off" placeholder="Email Address..." data-theme="e" required class="localStore">
         <input type="submit" value="Submit">
         </form>

and the next script -

$('#local_storage_form').submit(function(e) {
    e.preventDefault(); // prevent the form from attempting to send to the web server
    var $inputs = $('#local_storage_form :input');

    var values = {};
    $inputs.each(function() {
        values[this.name] = $(this).val();
    });

    localStorage.setItem('testForm', JSON.stringify(values));
});

I am still getting an 505 error on submit,and the values don't save on localStorage, What i need to change? What method/action to use?

Upvotes: 1

Views: 2226

Answers (1)

Peter
Peter

Reputation: 6669

Add an id to the form and change your jquery script into:

$('#local_storage_form').submit(function(e) {
    e.preventDefault(); // prevent the form from attempting to send to the web server
    var $inputs = $('#local_storage_form :input');

    var values = {};
    $inputs.each(function() {
        values[$(this).attr("name")] = $(this).val();
    });

    localStorage.setItem('testForm', JSON.stringify(values));
});

I don't know if jsfiddle.net disabled localStorage manipulation. I couldn't confirm it there to show you.

Upvotes: 1

Related Questions