Arushi Arora
Sep 1, 2018 · 2 min read

I am not able to add to the blockchain.

My code,

<!DOCTYPE html>

<html>

<head>

<title></title>

<meta charset=”utf-8" />

</head>

<body>

<script>
Web3 = require(‘web3’)
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 = [

{

“constant”: false,

“inputs”: [

{

“name”: “x”,

“type”: “uint256”

}

],

“name”: “set”,

“outputs”: [],

“payable”: false,

“stateMutability”: “nonpayable”,

“type”: “function”

},

{

“constant”: true,

“inputs”: [],

“name”: “get”,

“outputs”: [

{

“name”: “”,

“type”: “uint256”

}

],

“payable”: false,

“stateMutability”: “view”,

“type”: “function”

}

]

//contract address. please change the address to your own

var contractaddress = ‘0x5cab701a4777e421dfd52ef5a08ea7f5dd891091’;

//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 = “last inserted value into the blockchain is : “ + xname;

}

});

}

catch (err) {

document.getElementById(“xbalance”).innerHTML = err;

}

}

// function to add a new integer value to the blockchain

function setvalue() {

try {

// contract Abi defines all the variables,constants and functions of the smart contract. replace with your own abi

var abi = [

{

“constant”: false,

“inputs”: [

{

“name”: “x”,

“type”: “uint256”

}

],

“name”: “set”,

“outputs”: [],

“payable”: false,

“stateMutability”: “nonpayable”,

“type”: “function”

},

{

“constant”: true,

“inputs”: [],

“name”: “get”,

“outputs”: [

{

“name”: “”,

“type”: “uint256”

}

],

“payable”: false,

“stateMutability”: “view”,

“type”: “function”

}

]

//contract address. please change the address to your own

var contractaddress = ‘0x5cab701a4777e421dfd52ef5a08ea7f5dd891091’;

//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[1], 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”></div>

<h3>Insert and retrieve value on the blockchain</h3>

<br />

<table>

<tr>

<td></td>

<td>

<div id=”xbalance”></div>

</td>

</tr>

<tr>

<td>Insert a new value :</td>

<td>

<input id=”xvalue” type=”text” />

<input id=”Button1" type=”button” onclick=”setvalue()” value=”Add to Blockchain” />

</td>

</tr>

</table>

</center>

</body>

</html>

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade