This is my html, i put 1 number in a blockchain and i will see just the last inserted:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
</head>
<body>
<script>
window.onload = function () {
// check to see if user has metamask addon installed on his browser. check to make sure web3 is defined
if (typeof web3 === 'undefined') {
document.getElementById('metamask').innerHTML = 'You need <a href="https://metamask.io/">MetaMask</a> browser plugin to run this example'
}
// call the getvalue function here
getvalue();
}
//function to retrieve the last inserted value on the blockchain
function getvalue() {
try {
// contract Abi defines all the variables,constants and functions of the smart contract. replace with your own abi
var abi = []
//contract address. please change the address to your own
var contractaddress = '';
//instantiate and connect to contract address via Abi
var myAbi = web3.eth.contract(abi);
var myfunction = myAbi.at(contractaddress);
//call the get function of our SimpleStorage contract
myfunction.get.call(function (err, xname) {
if (err) { console.log(err) }
if (xname) {
//display value on the webpage
document.getElementById("xbalance").innerHTML = "primo: " + xname;
}
});
}
catch (err) {
document.getElementById("xbalance").innerHTML = err;
}
}
function setvalue() {
try {
// contract Abi defines all the variables,constants and functions of the smart contract. replace with your own abi
var abi = []
//contract address. please change the address to your own
var contractaddress = '';
//instantiate and connect to contract address via Abi
var myAbi = web3.eth.contract(abi);
var myFunction= myAbi.at(contractaddress);
//call the set function of our SimpleStorage contract
myFunction.set.sendTransaction(document.getElementById("xvalue").value, { from: web3.eth.accounts[0], gas: 4000000 }, function (error, result) {
if (!error) {
console.log(result);
} else {
console.log(error);
}
})
} catch (err) {
document.getElementById("xvalue").innerHTML = err;
}
}
</script>
<center>
<div id="metamask" class="container"></div>
<h3>Inserimento dati nella Blockchain Ethereum</h3>
<br />
<table class="table">
<div id="xbalance" class="container"></div>
<thead>
<tr>
<th scope="col">Inserisci token: </th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td><input id="xvalue" type="text" class="form-control" /></td>
</tr>
<tr>
<td><input id="Button1" type="button" class="btn btn-primary" onclick="setvalue()" value="Add to Blockchain" /></td>
</tr>
</tbody>
</table>
</center>
</body>
</html>
And my contract is:
pragma solidity ^0.4.24;
contract M {
uint256 storeddata;
function set(uint256 x) public {
storeddata = x;
}
function get() public view returns(uint256) {
return storeddata;
}
}