美元.未收到Get[]变量


$.get[] variable is not received

我尝试实现以下代码:

这是EditSet.php文件:

   <?php
function sanitizeString($var)
  {
   $var = strip_tags($var);
$var = htmlentities($var);
$var = stripslashes($var);
return mysql_real_escape_string($var);
  }
 $selectedset = sanitizeString($_GET['selectedset']);
   ?>
   <!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></title>
 <script type="text/javascript">
  $(document).ready(function() {

 $("#viewresult input:button").click(function () { 

     $.get("EditSet.php", "selectedset=Ceremony", function(){
 <?php
    $query =  "select imgid from imageinfo where setname='$selectedset' ORDER BY id DESC limit 1";
    $result = mysql_query($query);
    $row = mysql_fetch_object($result);
               $imgid = $row->imgid;
             echo "$('"#setimg'").fadeIn('"slow'").html('<img src='"edituploads/$imgid '"/>'); ";
     ?>                        
    });

        }); 
     }); 
  </script>
</head>
<body>
 <div style="float:left;border:1px solid  #aaa;"id="setimg"><img src="item.png"/></div>
 </body>
  </html>

我认为EditSet.php没有收到$get[]变量,因为当我改变以下内容:

  $query =  "select imgid from imageinfo where setname='$selectedset' ORDER BY id DESC limit 1";

:

    $query =  "select imgid from imageinfo where setname='Ceremony' ORDER BY id DESC limit 1";

右边的图像显示在"设置"div中,所以它工作得很好,get函数是否有问题,任何人都可以帮助我,非常感谢。我省略了数据库连接函数,所以如果我显示它,也不用担心。

这个问题令人困惑,因为您的AJAX正在调用脚本所在的页面。可以说是蛇在吃自己的尾巴。但是请记住,PHP和JavaScript不能像这样串联工作,因为PHP在AJAX运行之前被渲染到页面上。

如果您对另一个文件进行AJAX调用,则其不工作的原因变得更加明显。

index . php

<script>
$.get('ajax.php?meh=ohi',function(){
    alert("<?php echo $_GET['meh'];?>")
});
</script>

$_GET['meh']将在页面加载index.php时呈现,而不是ajax.php,您的JavaScript现在看起来像这样:

<script>
$.get('ajax.php?meh=ohi',function(){
    alert("")
});
</script>

并且$_GET['meh']将在风中丢失。

解决方案是把你的JS放在你的主页上,所有的PHP清理和MySQL放到一个单独的文件中。整个脚本看起来像-

index . php

<script>
$.get('ajax.php?selectset=ohi',function(data){
    alert(data)
});
</script>

ajax.php

<?php
// mysql stuff, sanitize string stuff, etc..
$selectset = $_GET['selectset'];
echo $selectset;
    // whatever you echo out here will be assigned to 'data' and alerted
?>