How to append (non duplicate) data to firebase db using javascript? - javascript

I am trying to use Firebase realtime db .I am trying to insert following form data in to firebase db but it does not use the name2 variable from form, instead it just insert name2 into db? Furthermore, how I append data to this db instead of updating same data every time i submit the form ? I considered using push but i don't want to create unique id/key each time in insert data!
Update: Now I am able to insert form name by using :
var playersRef = firebase.database().ref("players/"+ name2);
javascript:
function writeData5()
{
name2=document.getElementById("nameField2").value;
number2=document.getElementById("numberField2").value;
age2=document.getElementById("ageField2").value;
alert("name:"+name2+" number:"+number2+" age:"+age2);
var playersRef = firebase.database().ref("players/");
//playersRef.set ({john:{number:+number2,age: +age2}});
playersRef.set ({name2:{number:+number2,age: +age2}});
}
db structure I want to achive:

Related

JavaScript JQuery manipulating strange array type from database

I'm working with an old legacy system, and upgrading to php 8, and adding new functions.
Originally after login, the login page redirects you to dashboard.html, but once dashboard loads, it goes to PHP to recover the session data with OS stored in database, the search is by the session_id().
But the database gives this array
[{"session_data":"username = chronos;nome=> Pedro Henrique;email=> pedro.hsdeus#hotmail.com;senha=>ZmFzdDkwMDI=;data=> 25\/04\/2021;hora=> 18:02;uid=>e27fd9d043ec817b2f10ae59ad81b315;"}]
How to navigate by this using JQuery or JavaScript/JSON. The dashboard doesn't run any PHP code it is pure HTML and JavaScript
First of all. It is still a JSON string. So parse it.
const array = JSON.parse(data);
Now it is an array containing an object with 1 field. Get its content with
const content = array[0].session_data;
The content can be split up by the semicolon
const parts = content.split(';');
Now you have an array. Each field contains 1 part of the string. With the application of parts[index].substring() you can get the actual values and build up an object.
For example:
const object = {
username: parts[0].substring(parts[0].indexOf('='), parts[0].length),
name: parts[1].substring(parts[1].indexOf('=>'), parts[1].length),
email: parts[2].substring(parts[2].indexOf('=>'), parts[2].length)
}
and so on

Unable to get data from localstorage

I have faced a hurdle regarding the localstorage get and set.
Scenario: I am inserting data from form elements and caching it in an object and this object is pushed inside an array. So every time I am entering data into form elements those data are stored in an object and that object is pushed in an array. After pushing the object in the array userAry.push(newUser).
I have setItem the array in localstorage
localStorage.setItem('userDataStore',JSON.stringify(userAry))
Inside document.ready I have
$('#getResult').html(localStorage.getItem('userDataStore'))
Problem:
Inside document.ready the $('#getResult').html(sessionStorage.getItem('userDataStore')) is not showing the array. Instead it is only showing the recently added data.
If I fill up the form twice consecutively then after page refresh it is showing those 2 data. But if I add another data and refresh the page it should show 3 data instead it is showing the recently added data.
Code:
var newUser = {
userName : username,
userPhno : userphno,
userEmail : useremail,
userPass : userpass,
userConfirmpass : userconfirmpass,
}
userAry.push(newUser);
localStorage.setItem('userDataStore',JSON.stringify(userAry));
Inside document.ready I have
$('#getResult').html(sessionStorage.getItem('userDataStore'));
#NoviceCoder, To work with localstorage you can try below scenerio.
response = ['A', 'B', 'C', 'D'];
localStorage.setItem("marketPlaceNames", JSON.stringify(response)) // to set value
JSON.parse(localStorage.getItem("marketPlaceNames")) // to get value
in document ready, you should set up variable userAry too. Not only fill the HTML content. then when you push a new item it will add it to the others already created and then when you store the array, you store whole users not only newly added
userAry = sessionStorage.getItem('userDataStore');
$("#getResult").html(userAry);
In your code replace this line localStorage.setItem('userDataStore',JSON.stringify(userAry)); with sessionStorage.setItem('userDataStore',JSON.stringify(userAry));
Check it below code how to get, set and remove data from sessionStorage.
For example :
// Save data to sessionStorage
sessionStorage.setItem('key', 'value');
// Get saved data from sessionStorage
var data = sessionStorage.getItem('key');
// Remove saved data from sessionStorage
sessionStorage.removeItem('key');
// Remove all saved data from sessionStorage
sessionStorage.clear();

Using session variable to store a table with few rows and on submission of the final form , writes the data in SQLdatabase

I would like to know if it is possible to store a table with few rows in a session variable and then updating the information into a sql database on final submission in web2py .
Something like this:
session.records = [] # Initialize the list in the session
Later, add a record:
session.records.append(dict(field1=value1, field2=value2, ...))
Later, insert all records into the database:
ids = db.mytable.bulk_insert(session.records)
ids will be a list of the ids of the inserted records.

Fetch localStorage values to other page using id

I am storing the values in localStorage and the values are getting stored through input[type:hidden] and input[type:text].
JS:
$('.proceed_btn').on('click', function() {
// Blank to start with
var order = {};
// Loop through all inputs...
$('input[type="text"], input[type="hidden"]').each(function() {
// ...adding their values as properties
order[this.name] = this.value;
});
// Store that object in JSON format
localStorage.setItem("order", JSON.stringify(order));
});
I want to print these value in other page when the user redirects after submitting the form. I am working on ruby currently. There are many products in the website, so the information in the summary page gets rendered according to that. Is there any way to display specific form details in through there id's?
You can create a partial with .js.erb extension eg _order_partial.js.erb and retrieve the order object as thus:
order = JSON.parse(localStorage.getItem("order"));
// Loop through the object and print out
Then you can render the partial in any of the views file you want to use it.

How to get values from the store in extjs4

I have a store that fetches data from the zend server. I want to get the store records to do some customizations on my form. For getting data from store i am using the below code.
var index = Ext.StoreMgr.lookup('product.AttributeComboBox').find('abbr',4);
var reco = Ext.StoreMgr.lookup('product.AttributeComboBox').getAt(index);
Above snippet returns no records. Please let me know where i am wrong.
In your debugger check the store exists
Ext.StoreMgr.lookup('product.AttributeComboBox')
Check how many records are in the store
Ext.StoreMgr.lookup('product.AttributeComboBox').data.items
Check the records have parsed properly
What came from the server for the record
Ext.StoreMgr.lookup('product.AttributeComboBox').data.items[0].raw
How it get converted into the record
Ext.StoreMgr.lookup('product.AttributeComboBox').data.items[0].data
Can you show us more code ?
So far, seems ok but you will have to check if the store have been created and if it has all the records, just like RichH said.
To check if the Store exists I would do
var productStore = Ext.getStore('product.AttributeComboBox');
console.log(productStore );
To check if the store is loaded
console.log(productStore.getCount());
To find the record
console.log(productStore.findRecord('abbr','4'));

Categories