Passing javascript variable to php or direct to a field

1

How to pass the result of this script to a field and have already returned 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>
    
asked by anonymous 01.03.2015 / 04:20

1 answer

2

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

AJAX without jQuery: link

On 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 enter anything in the input id="field" and put the method to form for real in the script.

link

I hope I have helped!

    
01.03.2015 / 04:55