4
contract Student
{
uint256 index=0;
function Students(bytes32 name, uint256 adNo) returns (uint256 vid)
{
index++;
Name[index]=name;
AdmissionNo[index]=adNo;
vid=index;
}
mapping(uint256 => bytes32) public Name;
mapping(uint256 => uint256) public AdmissionNo;
}

the return value for vid is like 0xca297a8080be23ace753b20084fd853533af205c16bbfef7aa466208aa0341d5 when printing in html

<!doctype>
<html>
<head>
<script type='text/javascript'>
web3.eth.defaultAccount = web3.eth.accounts[0]
function get() {
    var index = document.getElementById('index').value;
    var name = contracts['Student'].contract.Name(index);
    var adNo = contracts['Student'].contract.AdmissionNo(index);
    document.getElementById('queryres').innerText = hex2a(name)+"   "+adNo;
    document.getElementById("queryres").appendChild(document.createElement("BR"));

}

function set() {
    var name = document.getElementById('name').value;
    var adNo = document.getElementById('adNo').value;
    var res = contracts['Student'].contract.Students(name,adNo);
    document.getElementById('queryres').innerText = res;
}
    function hex2a(hex)
        {
            //hex = document.calcform.hex.value;
            hex = hex.match(/[0-9A-Fa-f]{2}/g);
            len = hex.length;
            if( len==0 ) return;
            txt='';
            for(i=0; i<len; i++)
            {
                h = hex[i];
                code = parseInt(h,16);
                t = String.fromCharCode(code);
                txt += t;
            }
            return txt;
        }


</script>
</head>
<body bgcolor='#E6E6FA'>
    <h3>Students</h3>
<div>
Store:
    <input type='text' id='name'>
    <input type='number' id='adNo'>
    <button onclick='set()'>Save</button>
</div>
<div>
Query:
    <input type='number' id='index'>
    <input value='get' type='button' id='query' onclick='get()' />
    <div id='queryres'></div>
</div>
</body>
</html>

when clicking save button it should save and return index of the student

2

Transactions don't support return values in web3. The value you're displaying is the transaction hash, not the return value of the function you're calling.

Instead of trying to return the value from the transaction, you need to either provide a separate function call that looks up a record's index, or allow the caller to supply an ID for the record - such as a GUID. If you do the latter, be sure to prevent users from being allowed to overwrite existing records!

| improve this answer | |
  • 2
    Alternatively log an event with the number you want returned and have your javascript listen for that. – Edmund Edgar May 20 '16 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.