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

时间:2015-07-14 05:49:40

标签: 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
                        }
                    }
                }
            }
        }
    }   

?>

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

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

9 个答案:

答案 0 :(得分:1)

你好,你试着用jjery

来使用ajax

现在你的脚本只返回一个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>

答案 1 :(得分:0)

使用基本网址,然后将自定义路径粘贴到您的网页,如:

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

你应该避免使用Echoing脚本,使用带有Enclosing php标签的常规脚本块。

答案 2 :(得分:0)

您应该将警报放在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
}

答案 3 :(得分:0)

也许尝试在session_start()之后渲染一些html。 e.g。

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

这应该提供一个显示消息的页面。 虽然,在我看来,甚至更好:不要打破php,并使用php header()函数将你带到需要的相应页面。

答案 4 :(得分:0)

尝试使用它:

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

答案 5 :(得分:0)

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

您需要在当前脚本中实现window.location。

然后,在页面标记之前,您希望重定向用户:

$str = "$('#some-button').magnificPopup({
              items: {
                  src: 'path-to-image-1.jpg'
              },
          type: 'image'
         });";

print preg_replace('~\s~m', '', $str); // prints $('#some-button').magnificPopup({items:{src:'path-to-image-1.jpg'},type:'image'});

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

答案 6 :(得分:0)

您的位置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/

答案 7 :(得分:0)

此脚本将在同一页面上执行,但您必须使用此类

形式
<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');
                        }
                    }
                }
            }
        }
    }   
}
?>

答案 8 :(得分:-1)

您可以使用此代码

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

?>
当用户单击“确定”按钮时,

此代码重定向页面。 页面重定向前的警报按摩显示