Local storage: is that possible with Kirby


#1

Hallo

I am new in Kirby.
I try to build a page with frontend and everything works fine.
So I tried to implement a local storage to the frontend page.
But it doesnt work.

Do I need to write something into the controller to use the functions in the javascript?
The function doesnt let me set an item and doesnt store anything in it.
Does Kirby allow to use local storage in general?


#2

Local Storage is kept on the client side, so it is not (directly) accessible using PHP, which is a server side language. Having said that, you can store and retrieve data in Local Storage using JavaScript and use Ajax to send the data to the server.


#3

Okay, my LocalStorage works perfectly.

////////////////// Local Storage

$(function () {
	//Set Local Storage
	$('input[type="text"]').on('blur', function() {
		var input = $(this).val();
		localStorage.setItem(this.name, input);
	});
	$('textarea').on('blur', function() {
		var input = $(this).val();
		localStorage.setItem(this.name, input);
	});
	$('input[type="checkbox"]').on('click', function() {
		var name = $(this).attr('name');
		var val = $('input[name="'+name+'"]:checked').val();
		localStorage.setItem(this.name, val);
	});
	$('input[type="radio"]').on('click', function() {
		var id = $(this).attr('id');
		var val = $('input[id="'+id+'"]:checked').val();
		localStorage.setItem(this.id, val);
	});
	//Get Local Storage
	
	$('input[type="text"]').each(function(){
		var key = $(this).attr('name');
		var value = localStorage.getItem( key );
		if (value) {
			$(this).val( value );
		}
	}); 
	$('input[type="email"]').each(function(){
		var key = $(this).attr('name');
		var value = localStorage.getItem( key );
		if (value) {
			$(this).val( value );
		}
	});
	$('input[type="date"]').each(function(){
		var key = $(this).attr('name');
		var value = localStorage.getItem( key );
		if (value) {
			$(this).val( value );
		}
	});
	$('input[type="radio"]').each(function(){
		if ($(this).val() == localStorage.getItem($(this).attr('name'))){ 
			$(this).prop('checked',true); 
		} 
	}); 
	$('textarea').each(function(){
	   var key = $(this).attr('name');
	   var value = localStorage.getItem( key );
	   if (value) {
		   $(this).val( value );
	   }
	});
	$('input[type="checkbox"]').each(function(){
		if ($(this).val() == localStorage.getItem($(this).attr('name'))){ 
			$(this).prop('checked',true); 
		} 
	});
	//Remove Items
	$('button').on('click', function() {
		if (confirm('Are you sure?')) {
		   localStorage.clear();
		   submit();
       } else {
           return false;
       }
	});
});

Now i want to use the stored data to send it to the server in another page via Ajax. How can i send data via Ajax to use PUT?
Is it possible?


#4

Hi,
with Kirby is all possible what can realized with PHP.
What is PUT?


#5

PUT is a http method, like post, get etc.


#6

Thanks, I never used that so I havn’t remember :smile: .

@KriSch: I think it should work if you use PUT as type or you can override the header:

$.ajax({
    type: 'POST',
    dataType: 'json', 
    url: "yoururl",
    headers: {"X-HTTP-Method-Override": "PUT"},
    data: '{"foo": "bar"}'
});