Reputation: 657
Situation: I have two identical forms, on two seperate pages.
Page 1: The form on this page has a select field and on submission is supposed to take you to the second page and set the other identical form to the same selection
Page 2: This page has a form with a select field(no submit button). When the selections is changed I use jquery to get the contents of the option and show a div with a corrsponding class. Using the Code Below:
$('.result').hide();
$('#servicearea').change(function() {
$('.result').hide();
var optionValue = $ (this).attr('value');
$('#'+optionValue).show('fast');
});
The Problem:
I'm a novice when it comes to jQuery so I don't know where to start with this one. Please be nice. Thx!
Upvotes: 2
Views: 5599
Reputation: 226
You can try transfering data with localStorage.. think that should be what you're looking for (if you don't want to use php, or something similar..).
Quick example:
// first page
<script>
$('#submit').on('click', function (e) {
e.preventDefault();
var val1 = $('#val1').val();
var val2 = $('#val2').val();
var action = $('#form').attr('action'); // next page
window.localStorage.setItem('formData', JSON.stringify({
val1: val1,
val2: val2
}))
});
window.open(action, '_self');
</script>
// second page
<script>
$(window).on('load', function() {
var storage = window.localStorage.getItem('formData');
if (storage) {
storage = JSON.parse(storage);
$('#val1').val(storage.val1);
$('#val2').val(storage.val2)
}
})
</script>
This should grab fields (just change to yours), store in localStorage then open next page (should be action attr on form). On next page, we're looking in localStorage for values and populating the form.
Upvotes: 1
Reputation: 18078
Personally, I use the QueryParser()
constructor found here.
This has the following advantage over the gup()
function posted by Matthew Nie :
It also allows :
href
containing a query string built from the currently stored parameters.In conjunction with jQuery, use QueryParser()
as follows :
jQuery
and QueryParser()
on the page$("#formID [name='fieldName']").val($q.fieldName)
.Or to save having to hand-code each form field individually :
$.each($("#myform")[0].elements, function(index, element) {
$(element).val($q[element.name]);
});
Thus, the form on the page 2 will be populated with the values entered/selected on page 1.
Upvotes: 0
Reputation: 629
Using the idea @Maksym Kozlenko proposed of using a plain old HTML form for page one and submitting to the second page with the GET method. You can then use javascript to get the GET data you sent over using this function, I've been using this for a while and I found it on this site.
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
Upvotes: 0
Reputation: 10363
Why not to make things very simple with plain old HTML?
<form action="/second_page_url" method="GET">
... form fields ...
<input type="submit" />
</form>
It's also possible to use cookies, HTML5 local storage, but you should take into account that user can have more than one widnow/tab open.
You can also serialize form values using jQuery's serialize() method, but I don't see any reason doing this unless you send your data using AJAX request.
Upvotes: 0