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

mon*_*908 13 javascript php alert

    <?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
                        }
                    }
                }
            }
        }
    }   

?>
Run Code Online (Sandbox Code Playgroud)

它工作正常.但问题是:警报窗口显示到另一个网页,而不是在网页内显示它是可以的,但是将用户引导到另一个网页而没有内容可能很糟糕.如果您能帮助我,请解决如何解决此问题并使javascript警报窗口仅显示在同一网页上.实际上,我尝试过这样的解决方案..

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

Pra*_*tik 0

使用基本 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
}
?>
Run Code Online (Sandbox Code Playgroud)

并且您应该避免回显脚本,使用带有封闭 php 标签的常规脚本块。