0
I have a form
which makes a calculation, inserts it into the database and returns the value through a alert
.
I would like to replace this message from alert
by a modal
.
HTML
<form method="post" action="#">
<input type="hidden" name="acao" id="acao" value="cadastrar"/>
<input type="text" name="alt" id="altura" required><br><br>
<input type="text" name="peso" id="peso" required><br><br>
PHP
function calcularIMC($altura,$peso){
$imc = 0;
if($altura >0 && $peso >0){
$imc = $peso / ($altura * $altura);
}
echo '<script>';
echo 'alert("'.$imc.'");';
echo '</script>';
}
I tried to do it this way:
- I added the code php
after the tag </html>
;
- I added the variable responsible for the calculation: $imc
;
- I linked the button Enviar
with the modal
.
<form method="post" action="#">
<input type="hidden" name="acao" id="acao" value="cadastrar"/>
<input type="text" name="alt" id="altura" required><br><br>
<input type="text" name="peso" id="peso" required><br><br>
<button type="submit" id="Enviar" data-toggle="modal" data-target="#myModal">Enviar</button>
</form>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<?php echo'.$imc.';?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</body>
</html>
PHP
<?php
function calcularIMC($altura,$peso){
$imc = 0;
if($altura >0 && $peso >0){
$imc = $peso / ($altura * $altura);
}
return $imc;
}
?>
With that, when I click the button Enviar
(with all fields empty), it opens the modal and displays the '.$imc.'
like String.
When I enter the data and click on the button Enviar
the window becomes opaque (beginning of the modal effect) and quickly returns to normal. I believe that the modal
is being called, but somewhere along the way he closes.
How can I fix this?
A little question before... Why are you using this much echo? Really think it necessary?
– Lucas Henrique
@Lucashenrique, no. I was doing it wrong. I made some changes to the code, see the question again please :)
– GtGtGt
Are you using some framework to build your solution or are you doing it in "raw php" anyway? because from my experience with PHP and web, either you mount this on JS or already deliver the result as a view...
– LeandroLuk