Search code examples
javascripttransactionsattributesblockchainweb3js

How to dynamically set attribute in web3.eth.sendTransaction


I'm trying to create dynamic send ethereum transaction, which will send ether to specific address.

All attributes (from and value) are working correctly but "to" isn't (because var address1 won't be accepted and i'm trying to insert javascript variable).

I get reciever's address from ajax and it can be easily alerted but when i try to add it to "to" attribute, Metamask transaction window won't pop up. Oh and im getting this address from dropdown box when user decides. I've tried everything with quotations and cookies but none works.

Below in the first picture, im trying to set js variable dynamically and it's not working meanwhile on the second one, im trying to set it statically and it's working


Me trying to set variable dynamically and it's not working


Me setting variable static and it's working


function generateTransactionHash(){
    var amount = web3.toWei($('#vsota').val(), "ether");
    var uporabnik = $('#izbiraPrejemnika').val();

    var request = $.ajax({
        url: "model/izbiraPrejemnikaDAO.php",
        type: "POST",
        data: {uporabnik: uporabnik},
        success: function(reciever_address){
            var address = "'"+reciever_address+"'";
            web3.eth.sendTransaction({
                to: address,
                from: "<?php echo($dataEthAddress[0]) ?>",
                value: amount
            }, function(err, res){
                $('#transHash1').val(res);
                console.log(res);
            });

        }

    });
        return false;
    }

Solution

  • It seems really weird. Try to put a breakpoint right before you send the transaction to see the value of address1 after the allert(should be the same, but just in case). The address1 variable itself should NOT contain the double quotes.