2013-01-10 59 views
0

我創建了一個html表單用於註冊,並且當用戶成功註冊後,我希望它們自動重定向回我的index.html頁面,並提示彈出告訴他們已註冊成功。目前我的警報工作,但它只是在一個空白頁面打開。我嘗試過放入header ("location:../index.html"),但那沒有奏效,它只是直接引導我到頁面而沒有給我我想要的警報。有人能幫忙嗎?重定向做一個頁面並彈出提醒

<?php 
$con = mysql_connect("localhost","root","") or die(mysql_error()); 
$select_db = mysql_select_db("mayan",$con); 
if(isset($_POST['register'])) 
{ 
    $firstname=$_POST['first_name']; 
    $lastname=$_POST['last_name']; 
    $address=$_POST['address']; 
    $postcode=$_POST['postcode']; 
    $emailaddress=$_POST['emailaddress']; 
    $password=$_POST['password']; 
    $query = "select emailaddress FROM mayan_users where emailaddress='$emailaddress'"; 
    $link = mysql_query($query)or die(mysql_error()); 
    $num = mysql_num_rows($link); 
if ($num>0){ 
    echo 'Email already exists'; //email already taken 
} 

else { 
$insert_query = "insert into `mayan_users`(`firstname`,`lastname`,`address`,`postcode`,`emailaddress`,`password`) values('$firstname','$lastname','$address','$postcode','$emailaddress','$password')"; 
$result = mysql_query($insert_query)or die(mysql_error()); 



    if(success) 
{ 
    echo "<script type=\"text/javascript\">". 
     "alert('success');". 
     "</script>"; 
} 
    } 

    } 
?> 
+0

if(成功) - >這是一個函數或變量,如果這是一個變量,它應該是$ success。這一套是什麼時候? –

回答

1

使用此功能重定向時:

function redirect($url) { 
    if(!headers_sent()) { 
     //If headers not sent yet... then do php redirect 
     header('Location: '.$url); 
     exit; 
    } else { 
     //If headers are sent... do javascript redirect... if javascript disabled, do html redirect. 
     echo '<script type="text/javascript">'; 
     echo 'window.location.href="'.$url.'";'; 
     echo '</script>'; 
     echo '<noscript>'; 
     echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; 
     echo '</noscript>'; 
     exit; 
    } 
} 

所以,你要重定向到同一頁面,但與= XXX獲取價值

您的代碼將是這個樣子:

<?php 

if ($_GET['success'] == 1){ 
     echo "<script type=\"text/javascript\">". 
     "alert('success');". 
     "</script>"; 
} 
$con = mysql_connect("localhost","root","") or die(mysql_error()); 
$select_db = mysql_select_db("mayan",$con); 
if(isset($_POST['register'])) 
{ 
    $firstname=$_POST['first_name']; 
    $lastname=$_POST['last_name']; 
    $address=$_POST['address']; 
    $postcode=$_POST['postcode']; 
    $emailaddress=$_POST['emailaddress']; 
    $password=$_POST['password']; 
    $query = "select emailaddress FROM mayan_users where emailaddress='$emailaddress'"; 
    $link = mysql_query($query)or die(mysql_error()); 
    $num = mysql_num_rows($link); 
if ($num>0){ 
    echo 'Email already exists'; //email already taken 
} 

else { 
$insert_query = "insert into `mayan_users`(`firstname`,`lastname`,`address`,`postcode`,`emailaddress`,`password`) values('$firstname','$lastname','$address','$postcode','$emailaddress','$password')"; 
$result = mysql_query($insert_query)or die(mysql_error()); 



    if(success) 
{ 
    redirect('index.php?sucess=1'); 
} 
    } 

    } 
?> 
0

要由JavaScript重定向使用:

window.location = "http://www.google.com/"; 

但我建議增加一個鏈接,用戶點擊以及一些用戶可能禁用了JavaScript。我在重定向上放了一個超時,所以用戶有幾秒鐘的時間來閱讀這個頁面。例如:

if (success) { 
    echo "<script type=\"text/javascript\">" . 
     "setTimeOut(function() {" . 
     " window.location = 'http://www.google.com';" . 
      "}, 2500);" . 
     "</script>"; 
} 
0

你可以在索引頁面中做一個變量,如「index.php?success」。然後,只需通過if(isset($ _ GET ['success'])來檢查此請求)並添加一些登錄到該IF。

2

我使用此代碼將某人填寫到。網站

if(empty($errors)) 
{ 
    //send the email 

    echo ' 
     <script type="text/javascript"> 
      alert("YOUR MESSAGE"); 
      window.location.href = "http://www.yoursite.com";</script>';  
} 
-1

1)首先改變你的的index.html到PHP文件:的index.php

2)在的index.php文件的頂部寫

<?php 
    session_start(); 
?> 

3)在的index.php文件就可以了的主體:

任一打印消息

<?php 
    if(isset($_SESSION['message'])) { 
    echo $_SESSION['message']; 
    unset($_SESSION['message']); 
    } 
?> 

或警報用JavaScript

<?php 
    if(isset($_SESSION['message'])) { 
    echo '<script> alert(\'Success\'); </script>'; 
    unset($_SESSION['message']); 
    } 
?> 

4)在你給我們的這個動作文件

<?php 

    // START THE SESSION 
    session_start(); 

$con = mysql_connect("localhost","root","") or die(mysql_error()); 
$select_db = mysql_select_db("mayan",$con); 
if(isset($_POST['register'])) 
{ 
    $firstname=$_POST['first_name']; 
    $lastname=$_POST['last_name']; 
    $address=$_POST['address']; 
    $postcode=$_POST['postcode']; 
    $emailaddress=$_POST['emailaddress']; 
    $password=$_POST['password']; 
    $query = "select emailaddress FROM mayan_users where emailaddress='$emailaddress'"; 
    $link = mysql_query($query)or die(mysql_error()); 
    $num = mysql_num_rows($link); 
if ($num>0){ 
    $_SESSION['message'] = 'Email already exists'; //email already taken 
} 

else { 
$insert_query = "insert into `mayan_users`(`firstname`,`lastname`,`address`,`postcode`,`emailaddress`,`password`) values('$firstname','$lastname','$address','$postcode','$emailaddress','$password')"; 
$result = mysql_query($insert_query)or die(mysql_error()); 



    if($result) 
    { 
    $_SESSION['message'] = 'Success'; 
    } 
} 

} 
// redirect to main page 
header("Location: /"); 
?> 
相關問題