顺序jquery'pages',第三帧给出了引用错误

时间:2013-02-13 09:21:48

标签: javascript jquery function

我对javascript / jquery很新,并且一直在玩w3schools教程和jquery文档,并制作了一个页面,它将接受一些用户输入,然后通过javascript打印一些输出。我假设我可以修改它以按顺序工作,但是当第二页应该调用第三页时我得到

ReferenceError: $gp is not defined
[Break On This Error]   

$('#txtField2').load('getglycosites.php?q='+$gp+'r='+$('.glycotype').val());

如果有人能够在jquery脚本中使用$ _get变量,我将不胜感激。

网页代码:

第一页(test.php):

<html>
  <head>
    <title>LeidenGlycoPeptide DataBase</title>
    <script src="jquery-1.9.1.min.js"></script>
  </head>
  <script>
    $(document).ready(function() {
      $('.glycoprotein').change(function() {
        $('#txtField').load('getglycopeptides.php?gp='+$('.glycoprotein').val());
      });
    });
  </script>
  <body>
    <h1>Welcome to the LeidenGlycoPeptide DataBase</h1>
    <?php
      $link = mysql_connect("localhost","reader","") or die (mysql_error());
      mysql_select_db('leidenGlycoPeptide') or die ();
      $query = 'select protein from glycoPeptide';
      $result = mysql_query($query);
      mysql_close($link);
    ?>
    <form>
      <p>Select glycopeptide to search for (interactive dialog)</p>
      <?php
         echo"<select class=\"glycoprotein\">";
         echo"<option value=\"\">select glycoprotein</option>";
         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
           foreach ($line as $col_value) {
             echo"<option value=\"$col_value\">$col_value</option>";
           }
         } 
         echo"</select>";
       ?>
    </form>
    <br>
    <div id="txtField"></div>
  </body>
</html>

第二页(getglycopeptides.php):

<html>
  <head>
    <title>glyco</title>
    <script src="jquery-1.9.1.min.js"></script>
  </head>
  <script>
    $(document).ready(function() {
      $('.glycotype').change(function() {
        //NOTE $q/q are undefined
        $('#txtField2').load('getglycosites.php?q='+$gp+'r='+$('.glycotype').val());
      });
    });
  </script>
  <body>
    <?php
      // The next variable is the one I would like to use inside the jquery
      $gp=$_GET["gp"];
      $link = mysql_connect("localhost","reader","") or die (mysql_error());
      mysql_select_db("leidenGlycoPeptide",$link) or die();
      $query = "select glycoType from run,glycoPeptide where run.id = glycoPeptide.id and glycoPeptide.protein like '".$gp."'";
      $result = mysql_query($query);
    ?>
    <form>
      <?php
        echo "<select class=\"glycotype\">";
        echo "<option value=\"\">select glycosylation</option>";
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
          foreach ($row as $col_value)
          {
            echo"<option value=\"$col_value\">$col_value</option>";
          }
        }
        echo "</select>";
        mysql_close($link);
      ?>
    </form>
    <br>
    <div id="txtField2"></div>
  </body>
</html>

第三页(getglycosites.php):

<html>
   <head>
    <title>sites</title>
  </head>
  <body>
    <?php
      $gp=$_GET["gp"];
      $r=$_GET["r"];
      $link = mysql_connect("localhost","reader","") or die (mysql_error());
      mysql_select_db("leidenGlycoPeptide",$link) or die();
      $query = "select glycoSite from run,glycoPeptide where run.id = glycoPeptide.id and run.glycoType like '".$r."' and glycoPeptide.protein like '".$q."'";
      //echo $query;
      $result = mysql_query($query);
      echo "<select name=\"site\" onchange=\"foo\">";
      echo "<option value=\"\">select glycosite</option>";
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
      {
        foreach ($row as $col_value)
        {
          echo"<option value=\"$col_value\">$col_value</option>";
        }
      }
      echo "</select>";
      mysql_close($link);
    ?>
  </body>
</html>

PS:第三页目前只应该显示一个新的选择字段,但是一旦我开始工作,它将成为一个大顺序序列的一部分。

提前致谢

1 个答案:

答案 0 :(得分:0)

我能够修复&#39;这通过添加以下行(到jquery脚本):

var gp = '<?php echo htmlspecialchars($_GET['gp']); ?>';

使整个脚本成为第三页的一部分:

<script>
  var gp = '<?php echo htmlspecialchars($_GET['gp']); ?>';
  $(document).ready(function() {
    $('.glycotype').change(function() {
      $('#txtField2').load('getglycosites.php?q='+gp+'&r='+$('.glycotype').val());
    });
  });
</script>