Skip to content

Instantly share code, notes, and snippets.

@guibranco
Created December 8, 2018 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guibranco/17e72ed6098f2ed9d34aa2790a2263a5 to your computer and use it in GitHub Desktop.
Save guibranco/17e72ed6098f2ed9d34aa2790a2263a5 to your computer and use it in GitHub Desktop.
Loop de PHP & JS - Davidson Web - PHP Brasil - Facebook - https://www.facebook.com/groups/nao.tem.biscoito/permalink/10161478291675160/
<html>
<body>
<script>
function soma(numeroDoForm) {
var campos = document.querySelectorAll("input[id^='campo-" + numeroDoForm + "-']");
var resultado = document.getElementById("resultado-" + numeroDoForm);
var soma = 0;
for(var i = 0; i < campos.length; i++)
soma += campos[i].value * 1;
resultado.value = soma;
}
</script>
<?php
for ($i=0; $i < 3; $i++){
?>
<form name="form-<?php echo $i; ?>">
<input id="campo-<?php echo $i; ?>-1">
<input id="campo-<?php echo $i; ?>-2">
<input id="campo-<?php echo $i; ?>-3" >
<input id="resultado-<?php echo $i; ?>" onmouseover="soma(<?php echo $i; ?>)" readonly><br>
</form>
<?php } ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment