Hi, DaniWeb

I'm doing a registration page and i want the button "Send" add the information to the MySQL DB, then appear an alert box saying: "Regsitration succesfully!!" and in the end redirect to "index.php".

This is what i have:

<?PHP

//ligar base de dados
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'escola_musica';
mysql_select_db($dbname, $con);

/////////// ALUNO ///////////////
$nome = $_POST ['nome'];
$dia = $_POST ['dia'];
$mes = $_POST ['mes'];
$ano = $_POST ['ano'];
$data_nascimento = $dia .'-'. $mes .'-'. $ano;
$morada = $_POST ['morada'];
$cp1 = $_POST ['cp1'];
$cp2 = $_POST ['cp2'];
$cp_aluno = $cp1 .'-'. $cp2;
$curso = $_POST ['curso'];
$telemovel = $_POST ['tele'];
$email = $_POST ['email'];
$observacao = $_POST ['obs'];

///////////ENC.EDU.///////////////

$nome_ee = $_POST ['nome_ee'];
$morada_ee = $_POST ['morada_ee'];
$cp1_ee = $_POST ['cp1_ee'];
$cp2_ee = $_POST ['cp2_ee'];
$cp_ee = $cp1_ee .'-'. $cp2_ee;
$tele_ee = $_POST ['tele_ee'];
$email_ee = $_POST ['email_ee'];

include 'veref_aluno.php';
include 'veref_ee.php';

//inserir na base de dados
$sql = "INSERT INTO alunos (nome_aluno, data_nascimento, morada_aluno, cp_aluno, telemovel_aluno, email_aluno, curso, observacoes, nome_ee, morada_ee, cp_ee, telemovel_ee, email_ee)
		VALUES('". $nome ."', '". $data_nascimento ."', '". $morada ."', '". $cp_aluno ."', '". $telemovel ."', '". $email ."','". $curso . "','".  $observacao . "','". $nome_ee ."', '". $morada_ee ."', '". $cp_ee ."', '". $tele_ee ."', '". $email_ee ."')";


if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
 
?>

<script type="text/javascript">
 function show_alert()
 {
 alert("Inscrição feita com sucesso!");
 }
 </script>

I tried:

header('Location: http://www.example.com/');

Someone help me, plz.

Thank you
PF2G

Member Avatar for diafol

I'm assuming that this alert never shows because it's inside a function - nothing calls it?

if (!mysql_query($sql,$con)){
   die('Error: ' . mysql_error());
}else{
?>
<script type="text/javascript">
   alert("Inscrição feita com sucesso!");
</script>
<?php
}
?>

Gess nthat you get a 'header already send' error

That because headers have to be sende before any output and that includes javascript

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else header('Location: http://www.example.com/index.php');

Put the alert in the index.php (or use something better then an alert box)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.