Send Ether using Javascript and Html

In this post i will be using an html file and javascript to send Ether from one account to another account.
the Ideal scenario is "Donations". From one account to another account(Author/Organization).

In this process i will make use of Metamask and Web3 and Ropsten Test Net.

An Event listener waits for the load of the website and checks if its Web3 Compatible, before proceeding. For the Chrome browser to be Web3 Compatible, it needs to have Metamask installed.

Note: You cant run this on your computer as an Html File Due to browser security restrictions. you will need a web server such as Apache or IIS.


<html>
<head>
<title>Smart Contract Ethereum Aresh</title>
</head>
<body>
<fieldset>
    <label> Ether:
      <input type="text" id="amount"></input>
    </label>
    <button onclick="sendmoney()">Donate to Aresh</button>
    <div id="response"></div>
  </fieldset>
</body>

<script>
//--------------Part 1------------
//-----on Browser load-----
window.addEventListener('load', function() {

  // Check if Web3 has been injected by the browser:
  if (typeof web3 !== 'undefined') {
    // You have a web3 browser! Continue below!
     console.log('You have web3');
    //startApp(web3);
  } else {
    console.log('You DONT HAVE web3. Also Due to browser security restrictions, we cant communicate with dapps running on file: Please use a local server for development.');
    alert("Install Metamask");
     // Warn the user that they need to get a web3 browser
     // Or install MetaMask, maybe with a nice graphic.
  }

})

//---------------Part 2--------
//---On Mouse click-----
function sendmoney() {
      web3.eth.sendTransaction({
        from: web3.eth.coinbase,
        to: '0x56d4849d60864e8b46bdcb038c40108704fa85ad', //aresh account
        value: web3.toWei(document.getElementById("amount").value, 'ether')
      }, function(error, result) {
        if (!error) {
          document.getElementById('response').innerHTML = 'Success: <a href="https://testnet.etherscan.io/tx/' + result + '"> View Transaction </a>'
        } else {
          document.getElementById('response').innerHTML = '<pre>' + error + '</pre>'
        }
      })
    }
</script>
</html>

Comments

Popular posts from this blog

Solidity Hash of Structs (Testing hash uniqueness)

Is Making the Crypto Space Legally Compliant Paving the Road to Mass (Blockchain) Adoption?

Parity installation on Ubuntu Virtual Machine