Problem in return after sending data! HTML & PHP. I thought echo would appear when sending the data, but nothing appears

Asked

Viewed 36 times

0

Mesmo depois de inserir e enviar dados, ele volta pra tela inicial

<!DOCTYPE html>
<html>
<head>
<title>Exercicio 3</title>
</head>
<body>
<?php  
$km = $_POST['km'];
$valcombu =$_POST['valcombu'];
$gaston =$_POST['gaston'];

$km = 0;
$valcombu;
$gaston = 0;

$gaston = ($km / 8) * $valcombu;
echo "Você vai gastar R$" .$gaston;
?>

<form action="" method="POST">
    <label for="km" >Entre com a quilometragem percorrida</label><br>
        <input type="number" name="km">
<br><br>
    <label for="valcombu">Entre com o preço do combustivel</label><br>
        <input type="number" name="valcombu">
<br>            
        <input type="submit" name="Enviar">
        <input type="reset" name="Resetar">
</form>
</body>
</html>

2 answers

0


You are resetting the values after rescuing them, do as follows, entered default values before. I put some improvements to avoid Warning messages:

<!DOCTYPE html>
<html>
<head>
    <title>Exercicio 3</title>
</head>
<body>
<?php  

$km = 0;
$valcombu;
$gaston = 0;


$km = isset($_POST['km']) ? $_POST['km'] : 0;
$valcombu = isset($_POST['km']) ? $_POST['valcombu'] : 0;

$gaston = ($km / 8) * $valcombu;

if(!isset($_POST['km']) && !isset($_POST['valcombu'])) {

?>

<form action="" method="POST">
    <label for="km" >Entre com a quilometragem percorrida</label><br>
        <input type="number" name="km">
<br><br>
    <label for="valcombu">Entre com o preço do combustivel</label><br>
        <input type="number" name="valcombu">
<br>            
        <input type="submit" name="Enviar">
        <input type="reset" name="Resetar">
</form>

<?php } ?>

<p><?php echo "Você vai gastar R$" .$gaston; ?></p>

</body>
</html>
  • Still not returning anything, always back to the same home screen

  • It goes back to the same screen because the form action is calling the same screen. But if you observe, will appear the message "You will spend $ " with a calculated value. You can create another file that does this calculation, and call it in the action.

  • So, this I know should work, but the problem is that the exercise in question was to be done with php inside html, outside this I’m having the same problem with other 3 exercises.

  • I edited the answer, try it now the way I specified and see if it meets what you’re expecting.

  • It’s not working yet, I thought it would be because I’m using XAMPP, but even without it still comes back nothing but a home screen .

  • Put in question the prints of the screens you are getting.

  • File is with your extension as . php? Nothing after that.

  • Ahhh, that’s right, thank you. There’s a way to get Echo out from under the data without going to "another page" so to speak?

  • Yes, I edited the answer here, put the answer as correct to give the question as finalized

  • But now after I send the information it goes to a blank screen

  • Was inside the if, updated.

  • 1

    Well, it’s still going to a "new page" so to speak, but it has helped a lot with the real problem of the question, thank you very much

Show 7 more comments

0

try the following code, the code tested and is 100% functional

<!DOCTYPE html>
<html>
<head>
    <title>Exercicio 3</title>
</head>
<body>
<?php  
// Define um valor padrão para o gasto
$gasto = 0;

// Verifica se foi recebido as variaveis post
if(isset($_POST['km']) && isset($_POST['valorDoCombustivel'])) {
    $km = $_POST['km'];
    $valorDoCombustivel = $_POST['valorDoCombustivel'];

    // Atribui um novo valor ao gasto
    $gasto = ($km / 8) * $valorDoCombustivel;
}
?>

<form action="" method="post">
    <label for="km" >Entre com a quilometragem percorrida</label><br>
    <input type="number" name="km">
    <br>

    <label for="valcombu">Entre com o preço do combustivel</label><br>
    <input type="number" name="valorDoCombustivel">
    <br>            

    <input type="submit" name="Enviar">
    <input type="reset" name="Resetar">
</form>

<p>Você vai gastar R$ <?php echo $gasto; ?></p>
</body>
</html>

Important: If condition checks only if there is a variable and not if its value is a number, if an empty value is passed or no number will return error, now you add your validations as needed.

I hope I’ve helped.

Browser other questions tagged

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