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>";
}
     }

   }
?>
4

5 回答 5

2

在网站上填写表格后,我使用此代码将某人重定向回主页。

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

    echo '
        <script type="text/javascript">
            alert("YOUR MESSAGE"); 
            window.location.href = "http://www.yoursite.com";</script>';    
}   
于 2013-01-10T15:24:08.387 回答
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');
}
     }

   }
?>
于 2013-01-10T15:19:56.540 回答
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>";
}
于 2013-01-10T15:18:48.313 回答
0

您可以在索引页面中执行变量,例如“index.php?success”。然后只需通过 if(isset($_GET['success']) 检查此请求。并在该 IF 中添加一些登录信息。

于 2013-01-10T15:23:20.343 回答
-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: /");
?>
于 2013-01-10T15:26:37.463 回答