Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
galera, seguinte estou com um big de um problema...
Estou fazendo um simulador de financiamento, só que o imput "parcela" não está vindo formatado com os dados gerados...
Segue abaixo o codigo
<script LANGUAGE="Javascript">
function FormataReais(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;
key = String.fromCharCode(whichCode); // Valor para o código da Chave
if (strCheck.indexOf(key) == -1) return false; // Chave inválida
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 2) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len - 2, len);
}
return false;
}
//Fim da Função FormataReais -->
</script>
<style>
.simulador { font-family:arial; font-size:12px }
.simulador tr td{ font-family:arial; font-size:12px }
</style>
<?php
require_once('include/conexaoMS.php');
?>
<?php
$imovel_valor = $_GET['vl'];
if ($imovel_valor == '') $imovel_valor = $_POST['valor_imovel'];
$imovel_valor = str_replace ('.', '',$imovel_valor);
$imovel_valor = str_replace (',', '.',$imovel_valor);
$valor = $imovel_valor;
$valor = number_format($valor, 2, ',', '.');
$entrada = (($imovel_valor * 20)/100);
//$entrada = number_format($entrada, 2, ',', '.');
$entrada = str_replace ('.', '',$entrada);
$entrada = str_replace (',', '.',$entrada);
$valorEntrada = $entrada;
$valorEntrada = number_format($valorEntrada, 2, ',', '.');
$valor_financiado = ($imovel_valor - $entrada);
$valor_financiado = str_replace ('.', '',$valor_financiado);
$valor_financiado = str_replace (',', '.',$valor_financiado);
$valor_financiado = number_format($valor_financiado, 2, ',', '.');
if ( $_POST['acao'] == 1)
{
$meses = ($_POST['prazo_ano']*12);
$taxa_ano = 10.50;
$taxa_ano_nova = $taxa_ano/12;
$taxa_mes = $taxa_ano_nova/100;
$taxa = $taxa_mes;
$cf = pow((1 + $taxa), $meses);
$cf = (1 / $cf);
$cf = (1 - $cf);
$cf = ($taxa / $cf);
$parcela = ($valor_financiado * $cf);
echo ($parcela_valor);
$juros = ($taxa * 100);
//$juros = number_format($juros, 1, ',', '.');
//$parcela = str_replace ('.', '',$parcela);
//$parcela = str_replace (',', '.',$parcela);
$parcela = number_format($parcela, 2, ',', '.');
}
?>
<div>
<fieldset style="width:405px;padding:10px; margin-top:42px; position:absolute " id="fin_simulador">
<form action="simulador.php" method="post" name="enviar" >
<div style="display:block; position:relative; float:left; margin-top:-34px; "><img src="imagens/icoCalc.png"> </div>
<a name="label" href="#label" onfocus="javascript:document.entre_contato.nome.focus();"></a>
<div style="font-size:14px;color:#018471;font-weight:bold;margin-top:10px; text-align:center">Preencha os campos abaixo para simular o valor das parcelas <span style="font-size:9px ">( todos os campos obrigatórios )</span></div>
<div style="font-size:12px;color:#333333;">
<table class="simulador" width="400" cellpadding="0" cellspacing="0" border="0">
<tr>
<td colspan="3" height="10"> </td>
</tr>
<tr>
<td align="right" width="200" height="28">* Valor do imóvel (R$):</td>
<td width="5"> </td>
<td align="left">
<input type="text" id="valor_imovel" onkeypress="return(FormataReais(this,'.',',',event))" name="valor_imovel" value="<?php echo $valor?>" style="width:100px;height:19px; font-size:10px; vertical-align:middle; "/>
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td align="right" height="28">* Valor da entrada (R$): </td>
<td> </td>
<td align="left"><input type="text" onkeypress="return(FormataReais(this,'.',',',event))" alt="Valor da entrada (R$):" value="<?php echo $valorEntrada?>" id="valor_entrada" name="valor_entrada" style="width:100px;height:19px;font-size:10px; vertical-align:middle; " /></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td align="right" height="28">* Prazo do financiamento (anos): </td>
<td> </td>
<td align="left"><input name="prazo_ano" type="text" id="prazo_ano" style="width:100px;height:19px; font-size:10px; vertical-align:middle; " value="<?php echo $_POST['prazo_ano']?>" alt="Prazo do financiamento (anos):" /></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td align="right" height="28">* Valor a financiar (R$):</td>
<td> </td>
<td align="left"><input type="text" onkeypress="return(FormataReais(this,'.',',',event))" alt="Valor a Financiar" id="valor_financiar" value="<?php echo $valor_financiado?>" name="valor_financiar" style="width:100px;height:19px; font-size:10px; vertical-align:middle; " /></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td align="right" height="28">* Taxa de juros anual (%):</td>
<td> </td>
<td align="left">
<input name="financiamento" id="financiamento" type="text" value="10,50" style="width:100px;height:19px; font-size:11px; vertical-align:middle; " />
<span style="font-size:10px "> (média) </span></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td align="right" height="28">* Parcela mensal estimada (R$): </td>
<td> </td>
<td align="left">
<input id="parcela_estimada" name="parcela_estimada" onkeypress="return(FormataReais(this,'.',',',event))" alt="Parcela mensal estimada (R$)" maxlength="7" value="<?php echo $parcela?>" type="text" style="width:100px;height:19px; font-size:10px; vertical-align:middle; " />
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td align="center" colspan="3" height="35">
<input type="hidden" name="acao" id="acao" value="1">
<input name="Submit" type="submit" id="Calcular" style="border:1px solid #999999;background-color:#EAEAEA;color:#000000;text-align:center;cursor:pointer;" value="Calcular" />
</td>
</tr>
</table>
</div>
</form>
</fieldset>
</div>
Os outros campos estão normais, só que quando eu clico em calcular ele vai aparercer o valor no imput " Parcela " e esse campo não é formatado...
Isso acontece depois que comecei a puxar os valores que eu preciso do banco de dados...
Se eu setar os valores na mão como valor do imovel essas coisas a formatação funciona normal...
Alguem pode me ajudar com isso... Estou perdendo um bom tempo com isso já e não sei como resolve-lo.
Como podem ver eu também tentei setar por javascript mais quando o valor é gerado não formata...
Obrigado
Carregando comentários...