0
I have a page that performs a BMI calculation and saves some information in the bank.
The structure is divided into four files:
index.html
= Html code;
index.php
= Calls the index.html
;
banco.php
= Connects to the database;
cadastrar.php
= Performs the calculation, saves to the database and returns the message
The BMI message is given through a alert()
. When I click on "Ok", Alert closes and I continue on the whole page blank cadastrar.php
(page that picks up the data, makes the calculation and returns the user’s message).
I tried to use the expession location.href
and other options to try to redirect, but without success.
I would like to know how to do so that after I close the alert()
, the user returns to the home page (index)?
session_start();
include "banco.php";
$nome = "";
$altura = 0;
$peso = 0;
if(!empty($_POST))
{
$nome = $_POST['nome'];
$altura = $_POST['altura'];
$peso = $_POST['peso'];
cadastrar($nome,$altura,$peso);
calcularIMC($altura,$peso);
}
function cadastrar($nome,$altura,$peso)
{
try
{
$pdo = conectar();
$incluir = $pdo->prepare("INSERT INTO usuario("
. "nome, altura, peso) VALUES("
. ":nome, :altura, :peso)");
$incluir->bindValue(":nome", $nome);
$incluir->bindValue(":altura", $altura);
$incluir->bindValue(":peso", $peso);
$incluir->execute();
if($incluir->rowCount() > 0)
return true;
else
return false;
}catch(PDOException $e)
{
echo "Erro ao incluir na tabela categoria ".$e->getMessage();
}
}
function calcularIMC($altura,$peso){
$imc = 0;
if($altura >0 && $peso >0){
$imc = $peso / ($altura * $altura);
}
echo '<script language="javascript">';
echo 'alert("Sua quantidade de IMC é: '.$imc.'")';
echo 'location.href="index.html"'; //Tentei utilizar o location.href, mas sem sucesso
echo '</script>';
}
I made this change. The
alert()
does not shoot and page is also not redirected– Zkk
Here it works in several browsers, I have not tested all the code, only the last one, I gave any value to a variable "$imc" before the code. Press the CTRL+U keys on the page, and make sure this Javascript is being generated.
– mau humor
Here it has no effect, but tries to remove: language="javascript", or change to 'type='text/javascript'
– mau humor
When I add the
echo 'location.href="index.html";';
it displays the following error in php line 1: Uncaught Syntaxerror: Unexpected Identifier– Zkk
Did you put the ";" after the Alert? If the interpreter does not expect the "Location" (error translation) it is because he expects something else before, which may well be the semicolon I suggested.
– mau humor
It’s amazing, I always manage to do the hardest and hit me in the easiest rsrs It was the semicolon in the Alert. Thank you!
– Zkk