开发者

$.get[] variable is not received

开发者 https://www.devze.com 2023-04-13 09:11 出处:网络
I tried to implement the following code: this is EditSet.php file: <?php function sanitizeString($var)

I tried to implement the following code:

this is EditSet.php file:

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

I think the EditSet.php didn't receive the $get[] variable, because when I change the follwing:

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

to:

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

The right image is displayed in the "setimg" div, so it works fine, is there something wrong with the get function, any one could help me, thanks a lot. I have omit the database connect function, so don't worry if I did show it.


This problem is confusing because your AJAX is calling the page the script is on.. snake eating its tail so to speak. But keep in mind that PHP and JavaScript can't work in tandem like this because PHP is rendered onto the page before the AJAX is run.

If you make your AJAX call to a different file, the reason its not working becomes more apparent.

index.php

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

$_GET['meh'] will be rendered upon the page load of index.php, NOT ajax.php, and your JavaScript will now look like this:

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

And the $_GET['meh'] will be lost in the wind.

The solution is to put your JS on your main page, and all the PHP sanitizing and MySQL into a separate file. Whole script looking something like -

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

精彩评论

暂无评论...
验证码 换一张
取 消