Javascript警报窗口重定向到另一个网页

Javascript Alert Window Redirects to Another Webpage

本文关键字:另一个 网页 重定向 窗口 Javascript      更新时间:2023-09-26
    <?php
session_start();
include_once "connect.php";
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query =  mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
  if($fullName == "")
  {
?>
    <script>
      alert("Full Name is required!");
      window.location.href = "registeruser.php";
    </script>
<?php
  }
    else
    {
      if ($userName == "")
      {
?>
        <script>
          alert("Invalid username!");
          window.location.href = "registeruser.php";
        </script>
 <?php     
      }
        else
        {
          if($userName == $result['USERNAME'])
          {
?>
            <script>
              alert("Username already exists!");
              window.location.href = "registeruser.php";
            </script>
<?php
          }
            else
            {
              if ($emailAdd == $result['EMAIL']) 
              {
?>
                <script>
                  alert("Email address is required!");
                  window.location.href = "registeruser.php";
                </script>
<?php
              }
                else
                {
                  if ($passWord == "")
                  {
?>
                    <script>
                      alert("Username is required!");
                      window.location.href = "registeruser.php";
                    </script>
 <?php     
                  }
                    else
                    {
                      if($_POST['password']==$_POST['confirmpass'])
                      {
                        mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) 
                        VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
                        mysql_close();
?>    
                        <script>
                          alert("Registered Successfully!");
                          window.location.href = "index.php";
                        </script>
<?php
                      }
                        else
                        {
?>
                          <script>
                            alert("Password and Confirm Password do not match");
                            window.location.href = "registeruser.php";
                          </script>
<?php
                        }
                    }
                }
            }
        }
    }   
?>

很好。但问题是:警报窗口显示到另一个网页,而不是在网页这是可以的,但它看起来很糟糕,直接用户到另一个网页没有内容在那里。如果你能帮助我请如何解决这个问题,使javascript警报窗口只显示在同一网页上。事实上,我已经尝试了一些这样的解决方案。

if($fullName == "")
{
  echo '<script>';
  echo 'alert("Full Name is required!")';
  echo 'window.location.href = "registeruser.php"';
  echo '</script>';
}

您好,您是否尝试使用ajax与jquery

现在是你的脚本只是返回一个json对象说,如果一切正常//reg.php

session_start();
header('Content-type: application/json');
include_once "connect.php";
// result we return to the user
$response = ['error' => false, 'message' => ''];
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query =  mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
if($fullName == "") {
    $response['message'] = 'Full Name is required!';
    $response['error'] = true;
} else {
    if ($userName == "") {
        $response['message'] = 'Invalid username!';
        $response['error'] = true; 
    } else {
        if($userName == $result['USERNAME']) {
            $response['message'] = 'Username already exists!';
            $response['error'] = true;
        } else {
            if ($emailAdd == $result['EMAIL'])  {
                $response['message'] = 'Email address is required!';
                $response['error'] = true;
            } else {
                if ($passWord == "") {
                    $response['message'] = 'Username is required!';
                    $response['error'] = true;
                } else {
                    if($_POST['password']==$_POST['confirmpass']) {
                        mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD)  VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
                        mysql_close();
                        $response['message'] = 'OK';
                        $response['error'] = false;
                    } else {
                        $response['message'] = 'Password and Confirm Password do not match';
                        $response['error'] = true;
                    }
                }
            }
        }
    }
}
die(json_encode($response));

和//registration.php

<form method=POST action="reg.php" id="myForm">
<!--input goes here-->
</form>
<script src="//path to jquery"></script>
<script>
$(document).ready(() => {
    $('#myForm').on('submit', function (e) {
        e.preventDefault();
        $.post('reg.php', $(this).serialize())
            .done((datas) => {

                if(datas.erreur) {
                    alert(datas.message);
                } else {
                    alert('registration succesfully');
                }
            }).fail(() => {
                alert('connect error !!!');
            });
    });
});
</script>

使用基础url,然后将您的自定义路径粘贴到您的页面,如:

<?php
if($fullName == "")
{
?>
  <script>
   var MyBaseURL = <?php echo BASE_URL_CONSTANT?>
   var customURL = MyBaseURL."/registeruser.php";
   alert("Full Name is required!");
   window.location.href = customURL;
  </script>
<?php
}
?>

和你应该避免回显脚本,使用常规的脚本块带php标签。

也许尝试在session_start()之后呈现一些html。例如

<html>
<head>
</head>
<body>
<h1>Login Message</h1>
</body>
</html>

应该为消息提供要显示的页面。不过,在我看来,更好的做法是:完全不要跳出php,使用php header()函数将您带到所需的适当页面。

试着这样使用:

if($fullName == "")
{
    echo "<script language='javascript'>alert('Full Name is required!')</script>";
    echo "<script language='javascript'>window.location.replace('page.php?id=".$crmId."'); </script>";
}

正如其他人所说,您需要将逻辑移到HTML的末尾。其次,您需要在希望加载的页面上实现此逻辑,而不是在重定向的页面上。这是一个两步逻辑。例如:

你需要实现window。当前脚本的位置

然后,在你希望用户被重定向的页面标签前:

<?php
if($fullName == "")
{
  echo '<script>';
  echo 'alert("Full Name is required!")';
  echo '</script>';
}
?>
</body>

所以,这样做,在页面加载后,它会正确提示

您应该将警告放在registeruser.php页面上。让PHP脚本这样做:

header("Location: registeruser.php?message=" . urlencode("Invalid username"));

那么registeruser.php可以做:

<?php
if (isset($_GET['message'])) {
    ?>
    <script>
    alert(<?php echo json_encode($_GET['message']); ?>);
    </script>
    <?php
}

您的位置href语法错误。应该是这样的:

if($fullName == "")
{
  echo '<script>';
  echo 'alert("Full Name is required!")';
  echo 'window.location.href = SITE_URL."registeruser.php"';
  echo '</script>';
}

这里SITE_URL是你的项目根路径,如http://localhost/yourproject/

此脚本将在同一页面上执行,但您必须使用这样的形式

<form method=POST action="">
<!--input goes here-->
</form>

<?php
session_start();
include_once "connect.php";
if(isset($_POST[submit])){
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query =  mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
  if($fullName == "")
  {

  echo '    <script>
      alert("Full Name is required!");
      window.location.href = "registeruser.php";
    </script>';
  }
    else
    {
      if ($userName == "")
      {
        echo '   <script>
          alert("Invalid username!");
          window.location.href = "registeruser.php";
        </script>';
      }
        else
        {
          if($userName == $result['USERNAME'])
          {
          echo '  <script>
              alert("Username already exists!");
              window.location.href = "registeruser.php";
            </script>';
          }
            else
            {
              if ($emailAdd == $result['EMAIL']) 
              {
                echo '<script>
                  alert("Email address is required!");
                  window.location.href = "registeruser.php";
                </script>';
              }
                else
                {
                  if ($passWord == "")
                  {
                   echo ' <script>
                      alert("Username is required!");
                      window.location.href = "registeruser.php";
                    </script>';
                  }
                    else
                    {
                      if($_POST['password']==$_POST['confirmpass'])
                      {
                        mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) 
                        VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
                        mysql_close();
                      echo '   <script>
                          alert("Registered Successfully!");
                          window.location.href = "index.php";
                        </script>';
                      }
                        else
                        {
                         echo '  <script>
                            alert("Password and Confirm Password do not match");
                            window.location.href = "registeruser.php";
                          </script>';
                        }
                    }
                }
            }
        }
    }   
}
?>

或者你可以像这样使用 registration.php

<?php
 if(isset($_GET[fname]) && $_GET[fname]==1)
    echo ' <script>
          alert("Full Name is required!");
        </script>';
  if(isset($_GET[uname]) && $_GET[uname]==1 )
      echo '   <script>
          alert("Invalid username!");
        </script>';
  if(isset($_GET[uname]) && $_GET[uname]==2 )
    echo '  <script>
              alert("Username already exists!");
            </script>';
  if(isset($_GET[pwrd]) && $_GET[pwrd]==2 )
  echo '  <script>alert("Password and Confirm Password do not match");
                          </script>';
  if(isset($_GET[pwrd]) && $_GET[pwrd]==1 )
     echo '  <script>alert("Password is invalid");
                          </script>';
    if(isset($_GET[mail]) && $_GET[mail]==1 )
      echo '  <script>alert("invalid mailid");
                          </script>';
?>
<form method=POST action="reg.php">
<!--input goes here-->
</form>

reg.php

<?php
session_start();
include_once "connect.php";
if(isset($_POST[submit])){
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query =  mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
  if($fullName == "")
  {
              header('Location:registration.php?fname=1');

  }
    else
    {
      if ($userName == "")
      {
                header('Location:registration.php?uname=1');
      }
        else
        {
          if($userName == $result['USERNAME'])
          {
           header('Location:registration.php?uname=2');
          }
            else
            {
              if ($emailAdd == $result['EMAIL']) 
              {
               header('Location:registration.php?mail=1');
              }
                else
                {
                  if ($passWord == "")
                  {
                                header('Location:registration.php?pwrd=1');
                  }
                    else
                    {
                      if($_POST['password']==$_POST['confirmpass'])
                      {
                        mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) 
                        VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
                        mysql_close();
                      echo '   <script>
                          alert("Registered Successfully!");
                          window.location.href = "index.php";
                        </script>';
                      }
                        else
                        {
           header('Location:registration.php?pwrd=2');
                        }
                    }
                }
            }
        }
    }   
}
?>

可以使用以下代码

    <?php
session_start();
include_once "connect.php";
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query =  mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
  if($fullName == "")
  {
?>
    <script>
      if(alert("Full Name is required!"))
      {
            window.location.href = "registeruser.php";
        }
    </script>
<?php
  }
    else
    {
      if ($userName == "")
      {
?>
        <script>
          if(alert("Invalid username!"))
          {
            window.location.href = "registeruser.php";
            }
        </script>
 <?php     
      }
        else
        {
          if($userName == $result['USERNAME'])
          {
?>
            <script>
              if(alert("Username already exists!"))
              {
                    window.location.href = "registeruser.php";
                }
            </script>
<?php
          }
            else
            {
              if ($emailAdd == $result['EMAIL']) 
              {
?>
                <script>
                  if(alert("Email address is required!"))
                  {
                        window.location.href = "registeruser.php";
                    }
                </script>
<?php
              }
                else
                {
                  if ($passWord == "")
                  {
?>
                    <script>
                      if(alert("Username is required!"))
                      {
                            window.location.href = "registeruser.php";
                        }
                    </script>
 <?php     
                  }
                    else
                    {
                      if($_POST['password']==$_POST['confirmpass'])
                      {
                        mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) 
                        VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
                        mysql_close();
?>    
                        <script>
                          if(alert("Registered Successfully!"))
                          {
                                window.location.href = "index.php";
                            }
                        </script>
<?php
                      }
                        else
                        {
?>
                          <script>
                           if(alert("Password and Confirm Password do not match"))
                            {
                                  window.location.href = "registeruser.php";
                            }
                          </script>
<?php
                        }
                    }
                }
            }
        }
    }   
?>

此代码在用户单击ok按钮时重定向页面。提示按摩显示前页面重定向