Passing javascript variable to php or direct to a field

Asked

Viewed 546 times

1

How to pass the result of this script to a field and already returning formatted in Real Currency

</head>
<body>
<input type="checkbox" id="iv"/></br>
<input type="text" id="campo" value="10"></br>
<select name="children-qnt" id="children-qnt">
<option value="0">0</option>
<option selected value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Result: <span id="resultado">10</span>

<script type="text/javascript">
    var $campo = byId('campo'),
$select= byId('children-qnt'),
$checkbox = byId('iv');

function byId(element){
return document.getElementById(element);
}

function updateResult(){
var result =  parseInt($campo.value) *
            parseInt($select.value);
byId('resultado').innerHTML = $checkbox.checked ? result * 2 : result;


}

$campo.addEventListener('keyup', updateResult);
$select.addEventListener('change', updateResult);
$checkbox.addEventListener('change', updateResult);
</script>

1 answer

2


I’m not sure of your question, but if you want to pass the value of a JS variable to PHP you can use AJAX.

AJAX without jQuery: http://youmightnotneedjquery.com/#post

About passing the value to a field, your Script already does this, it passes the value to the field <span id="resultado"><!-- valor que fica aqui vem via script --></span>.

I added a test for when it does not type anything in input id="field" and put the form method for real in the script.

http://jsfiddle.net/kovLtn9k/1/

I hope I’ve helped!

  • Ok this was just what I needed more not this adding up the last decimal places and when I put id in the field does not appear for anything exe: <input type="text id="result" />

  • You cannot and should not put two identical id’s on the same page, and the id="result" you already have and as your script is using it, can and will give conflict! About the fact that it is not adding up the decimals, this happens by the command parseInt, 'Cause you’re telling him to take only the integers of value, which means if you have 10.50 he’ll take the whole 10!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.