通过url传递变量

时间:2014-03-24 11:13:16

标签: javascript php variables url

我遇到了问题我不知道发生了什么事,可能只是一个愚蠢的事情,但我需要你的帮助。

如果得到一个非常简单的登录文件(开始)就是这个:

<?php
  include("conex.phtml");
  $link=Conectarse();
  $username = $_POST['username'];
  $pass =  $_POST['pass'];

  $city = "";
  $latitude = "";
  $longitude = "";
  //$obj = array();
  $empty="empty";
  $rows0="rows0";
  $id="";

  $result=mysql_query("select * from user where username='" . $username . "' and password=" . $pass, $link);
  if(!$result){
    header('Location: http://www.ihaveseen.org/index.html');    
  }else{
    if($rs = mysql_fetch_array($result)){   
      if ($rs==''){
        header('Location: http://www.ihaveseen.org/index.html');   
      } else {
        $id = $rs['id'];
        echo'<script>window.location="http://www.ihaveseen.org/main.php?id=hola";</script>';
        /*header('Location: http://www.ihaveseen.org/main.php?id=hola');*/
        //echo '' . $rs['id'] . '';
        //header('Location: check_incoming.php?id=' . $id); 
      }
    }else{
      header('Location: http://www.ihaveseen.org/index.html');  
    }   
  }

  mysql_close($link);
  exit();
?>

我正在尝试通过url传递一个名为id = hola的变量,非常简单,对吧?当我转到下一个文件“main.php”时,代码会在javascript脚本中向我发送语法错误。

<?php 
  $id = $_GET['id'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Woyou</title>
  </head>
  <link rel="stylesheet" href="./styles/styles16.css" type="text/css" id="css"/>
  <script type="text/javascript" charset="UTF-8">
    var id = getUrlVars()["id"];
    alert("hola");
  </script>
  <body onload="" onunload="GUnload();" onkeydown="" onkeyup="" id="body">
    <div id="guarda" style="display: none;"></div>
    <iframe src='woyou.html' id="woyou_frame" name="woyou_frame" frameborder="0" scrolling="no"></iframe>
  </body>
</html>

我做错了什么?

4 个答案:

答案 0 :(得分:1)

您可以直接获取值:

<script type="text/javascript" charset="UTF-8">
    var id = '<?php echo $id ?>';
    alert("hola");
    </script>

答案 1 :(得分:0)

重新编写html,如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Woyou</title>
    <link rel="stylesheet" href="./styles/styles16.css" type="text/css" id="css"/>
    <script type="text/javascript" charset="UTF-8">
      var id = getUrlVars()["id"];
      alert("hola");
    </script>
  </head>
  <body onload="" onunload="GUnload();" onkeydown="" onkeyup="" id="body">
    <div id="guarda" style="display: none;"></div>
    <iframe src='woyou.html' id="woyou_frame" name="woyou_frame" frameborder="0" scrolling="no"></iframe>
  </body>
</html>

您有两个开放的html代码,scriptlink代码不在head标记中,就像它应该的那样。

答案 2 :(得分:0)

您的页面中未定义getUrlVars。只需将其粘贴到您的脚本标记(顶部)

即可
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

答案 3 :(得分:0)

考虑从

更正查询
$result= mysql_query("select * from user where username='" . $username . "' 
         and password=" . $pass, $link);

$result= mysql_query("select * from user where username='" . $username . "' 
         and password='" . $pass."'", $link);