-2

假设我有如下代码:

<?php 
    include ('include/connectdb.php');
    if(isset($_POST['tbn_submit'])){
        $username = $_POST['text_username'];
        $password = $_POST['text_password'];
        $query = "SELECT * FROM tblusers WHERE `user_username`='".$username."' AND user_password='".$password."'";
        $res = mysql_query($query) or die (mysql_error());
        if($res){
            if(mysql_num_rows($res)){
                $user = mysql_fetch_assoc($res);
                if($user['user_possition'] == "Admin"){
                    echo '<script type="text/javascript">window.location = "view_form.php"</script>';
                }
                else if($user['user_possition'] == "User"){
                    header("location:view_edit_uid.php?uid=".$user['user_id']);
                    //in this point I need to use javascript instead of php code but I do not know how to write it
                }                                       
                else if($user['user_possition'] == "R1"){
                    echo '<script type="text/javascript">window.location = "view_form_res1.php"</script>';
                }
                else if($user['user_possition'] == "R2"){
                    echo '<script type="text/javascript">window.location = "view_form_res2.php"</script>';
                }                               
            } 
            else {
                echo '<tr><td colspan="2"><center><b style="color:red;">Invalide username or password, please check again!</b></center></td></tr>';             
            }
        } 
    }
?>

问题

我需要$user['user_id']在 javascript 中使用

`echo '<script type="text/javascript">window.location = "view_edit_uid.php?uid=".$user['user_id']</script>';` 

但它不起作用,因为它不知道$user['user_id']javascript,那么我该如何解决这个问题?

请任何人帮助我,谢谢。

4

4 回答 4

3

您必须像任何 PHP 变量一样回显它。

<script type="text/javascript">
  window.location = "view_edit_uid.php?uid=<?php echo $user['user_id']; ?>";
</script>

或者使用 PHP 专门重定向...您必须在发送任何其他标头之前执行此操作。

header( 'Location: view_edit_uid.php?uid=' . $user['user_id'] ) ;
于 2012-11-14T06:44:07.733 回答
1
echo '<script type="text/javascript">window.location = "view_edit_uid.php?uid='.$user['user_id'].'";</script>';
于 2012-11-14T06:53:51.327 回答
1
echo '<script type="text/javascript">window.location = "view_edit_uid.php?uid='.$user['user_id'].'"</script>';

你遗漏了一些“和”。

于 2012-11-14T06:48:43.147 回答
1

是的,您可以使用window.location

else if($user['user_possition'] == "User"){
    //header("location:view_edit_uid.php?uid=".$user['user_id']);
    echo '<script type="text/javascript">window.location = "view_edit_uid.php?uid='.$user['user_id'].'"</script>';
}
于 2012-11-14T06:50:29.353 回答